Check in C++ whether signal in QML file exists.
-
Read this post, but it is not helpful: Signal exists
I wonder whether it is possible to detect whether signal in QML files exists before connecting it in C++.
I create a qml component in c++ and try to connect a signal between qml and c++:QQmlComponent component(qmlEngine, QUrl::fromLocalFile("example.qml")); if(component.isError()) { return; } QQmlContext* rootItemContext = QQmlEngine::contextForObject(rootWindow); QQuickItem *object = qobject_cast<QQuickItem*>(component.beginCreate(rootItemContext)); if(object != nullptr) { QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); object->setParentItem(rootWindow->contentItem()); object->setParent(qmlEngine); //if(SIGNAL(generateEvent(QVariant, QString) exists) QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString))); component.completeCreate(); }
Is it possible to check (during run-time) if signal in qml file exists before connecting it?
-
Thank you guys, that is working solution for me:
int signalExists = object->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("generateEvent(QVariant, QString)")); if(signalExists != -1) QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
I had to pass full signature with arguments: (QVariant, QString), without them signal was not found.
@J-Hilk
Yes, it is a working solution but I want to check if the signal exists before connecting, because I want to get rid of warning in case the signal does not exist. -
You can check from Metaobject. You can see the method indexOfSignal(..).
Also i'm not sure why do you want to do this. -
@ChrisTof said in Check in C++ whether signal in QML file exists.:
Is it possible to check (during run-time) if signal in qml file exists before connecting it?
You can check this
QMetaObject
, like this:index = object->metaObject()->indexOfSlot(QMetaObject::normalizedSignature("generateEvent")); if (index == -1) { qWarning("Wrong slot name!"); } else { // do connect... }
-
@ChrisTof said in Check in C++ whether signal in QML file exists.:
QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
you're already using Qt4 Syntax so the connection will be created at run time, simply store the return value of
QObject::connect
and check if it was successfulQMetaObject::Connection c = QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString))); if(c){ //Successful connected }
-
Thank you guys, that is working solution for me:
int signalExists = object->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("generateEvent(QVariant, QString)")); if(signalExists != -1) QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
I had to pass full signature with arguments: (QVariant, QString), without them signal was not found.
@J-Hilk
Yes, it is a working solution but I want to check if the signal exists before connecting, because I want to get rid of warning in case the signal does not exist. -
@ChrisTof said in Check in C++ whether signal in QML file exists.:
I want to get rid of warning in case the signal does not exist.
If you're using Qt 5, go and use the signal & slot new syntax. You'll have error checking at compile-time for free...
Counter a, b; QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
-
@Pablo-J-Rogina
I don't think Qt5 Syntax works on signals that you define in the QML file only with signals defined in the underlaying c++ class.However, I'm not entirely sure! 🤷♂️