QObject :: connect: there is no such signal when connecting qml-signals in C ++ Qt 5.3

I am new to using the Qt framework. I am not sure where I am going wrong. I tried to look at many related materials, but still could not understand.

I get "QObject :: connect: There is no such signal error ..." while I declared the signal in a qml file.

Here is the code:

int main(int argc, char *argv[]) { QApplication app(argc, argv); //QDeclarativeView view; QQmlApplicationEngine engine; testclass dsc; QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///test.qml"))); while(component.isLoading()); if (component.isError()) { qWarning() << component.errors(); } QObject *object = component.create(); QQuickItem *item = qobject_cast<QQuickItem*>(object); QObject::connect(item,SIGNAL(dsa(QVariant)),&dsc,SLOT(testslot(QVariant))); QObject::connect(&dsc,SIGNAL(dummysignal(QVariant)),&dsc,SLOT(testslot(QVariant))); dsc.dummysignal(&dsc); qDebug("Entered :"); engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); return app.exec(); } 

qml file : test.qml

 Item { width: 800 height: 500 signal dsa(var obj) SystemPalette { id: palette } } 

Testing class : testclass.cpp

 #include <QObject> class testclass: public QObject { Q_OBJECT public: explicit testclass(QObject *parent = 0); signals: void dummysignal(QVariant); public slots: void testslot(QVariant); }; 

I get this error:

 QObject::connect: No such signal test_QMLTYPE_0::dsa(QVariant) in .. 
+5
source share
3 answers

The problem is that you declare the dsa signal parameter as a type of "var", which is considered the javascript value for the qml mechanism. So this spreads to C ++ as a QJSValue , and the signature of the signal you're trying to connect to is actually dsa(QJSValue) .

If you want the signature to be dsa(QVariant) , change the declaration of your signal in test.qml as follows:

 // test.qml Item { signal dsa(variant obj) width: 800 height: 500 SystemPalette { id: palette } } 

This should allow you to connect as you tried to execute the statement

 QObject::connect(item,SIGNAL(dsa(QVariant)),&dsc,SLOT(testslot(QVariant))); 

(But first you must update the signature of your slot to void testslot(QVariant); ... otherwise you will have the same problem on the flip side with the error "there is no such slot")

FWIW, here is a useful trick for debugging "there is no such signal / slot" errors:

 // Assuming you've instantiated QQuickItem* item // This will print out the signature for every signal/slot on the object // Make sure you include <QMetaObject>, <QMetaMethod> const QMetaObject* metaObj = item->metaObject(); for (int i = 0; i < metaObj->methodCount(); ++i) { QMetaMethod method = metaObj->method(i); qDebug() << method.methodSignature(); } 
+9
source

Add signal and slot

 #include <QObject> class testclass: public QObject { Q_OBJECT public: explicit testclass(QObject *parent = 0); signals: void dsa(QVariant parameter) { //some code } public slots: void testslot(QVariant parameter) { //some code here } void testslot(); }; 
+1
source

QVariant was a suitable type for use in Qt 5.2 to display var signal parameters, but it was changed in Qt 5.3 to instead switch to QJSValue : Change the C ++ parameter type used for var parameters in declared QML signals

Although this was returned in Qt 5.4, which will again use QVariant for var signal parameters: Unmap var signal parameters in QJSValue

+1
source

Source: https://habr.com/ru/post/1207029/


All Articles