QByteArray losing first element Qt 6.8
-
wrote on 5 Apr 2025, 21:16 last edited by
I am facing a weird issue.
I am using QMetaMethod to get signal signature. Debugging the signal Signature gives the correct value. However, on connect it loses the first letter.
QMetaMethod signal = metaProperty.notifySignal(); QByteArray signalName = signal.name(); QByteArray signalSignature = signal.methodSignature(); qDebug() << "Signal name:" << signalName; qDebug() << "Signal signature:" << signalSignature; connect(obj, signalSignature, this, SLOT(onPropertyChanged()));
This is the output.
Signal name: "valueChanged" Signal signature: "valueChanged(int)" valueChanged(int) qt.core.qobject.connect: QObject::connect: No such signal cTestDataPoint::alueChanged(int) qt.core.qobject.connect: QObject::connect: (sender name: 'TestDataPoint_1')
I have also tried using
const char* signalStr = signalSignature.constData();
and passing the signalStr instead. However, it has the same result. The conversion to char* printed gives correct name too, but on connect it loses first char.
Any ideas?
-
I am facing a weird issue.
I am using QMetaMethod to get signal signature. Debugging the signal Signature gives the correct value. However, on connect it loses the first letter.
QMetaMethod signal = metaProperty.notifySignal(); QByteArray signalName = signal.name(); QByteArray signalSignature = signal.methodSignature(); qDebug() << "Signal name:" << signalName; qDebug() << "Signal signature:" << signalSignature; connect(obj, signalSignature, this, SLOT(onPropertyChanged()));
This is the output.
Signal name: "valueChanged" Signal signature: "valueChanged(int)" valueChanged(int) qt.core.qobject.connect: QObject::connect: No such signal cTestDataPoint::alueChanged(int) qt.core.qobject.connect: QObject::connect: (sender name: 'TestDataPoint_1')
I have also tried using
const char* signalStr = signalSignature.constData();
and passing the signalStr instead. However, it has the same result. The conversion to char* printed gives correct name too, but on connect it loses first char.
Any ideas?
@mishaalnaeemdev said in QByteArray losing first element Qt 6.8:
Any ideas?
You don't use SIGNAL() macro so you just pass the plain signature to QObject::connect() which is wrong. SIGNAL() and SLOT() are there for a reason...
https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qobjectdefs.h#n52
-
wrote on 6 Apr 2025, 14:38 last edited by
I have tried that,
connect(obj, SIGNAL(signalSignature), this, SLOT(onPropertyChanged()));
but I end up getting
qt.core.qobject.connect: QObject::connect: Parentheses expected, signal cTestDataPoint::signalSignature
Although, that issue should have been with signal name, not signal signature. Both are giving the same issue
-
Lifetime Qt Championwrote on 6 Apr 2025, 14:46 last edited by Christian Ehrlicher 26 days from now
Since it's a compile time macro which combines two constant char arrays it won't work with a non-constant (at compile time) QByteArray. Simply look at the macro what it is doing and do it in your code when you want to do such a strange connect by yourself.
What are you trying to achieve at all? -
Hi,
Beside the good question of @Christian-Ehrlicher about your use case, if you want to use meta objects, then do it all the way for both signals and slots. That will make your life easier.
5/5