QObject::connect: No such signal while connecting qml signals in c++ Qt 5.3

atin

I am a newbie in using Qt framework. I am not sure where I am going wrong. I tried looking at many related material but still could not figure it out.

I am getting "QObject::connect: No such signal error.." while I have declared a 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 }
}

Test 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 am getting this error:

QObject::connect: No such signal test_QMLTYPE_0::dsa(QVariant) in ..
dbrianj

The problem is that you're declaring the dsa signal parameter as a 'var' type, which is considered a javascript value by the qml engine. So this gets propagated into c++ as a QJSValue, and the signature of the signal you're trying to connect with is actually dsa(QJSValue).

If you want the signature to be dsa(QVariant), change your signal declaration 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 were trying with the statement

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

(But first you should update the signature of your slot to void testslot(QVariant);...otherwise you'll just have the same problem on the flip side with a 'no such slot' error)

FWIW, here's a useful trick for debugging '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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Qt: Specifying multiple connection types with QObject::connect

From Dev

Is it possible to disconnect all slots from a signal in Qt5 QML?

From Dev

SIGNALS and SLOTS in QT5

From Dev

Qt QObject::connect() function cannot be connected

From Dev

Qt Connect signals with different arguments

From Dev

Sending a signal to a QML item from C++ (Qt5)

From Dev

How to connect a qml child component signal to a c++ slot

From Dev

Qt: connecting signals and slots from text

From Dev

QML signals connecting

From Dev

Qt C++ connect signal with non-void signature to lambda

From Dev

Qt connect() without QObject or slots

From Dev

QObject::connect: No such signal(classname)(signalname)(atribure)

From Dev

Wait for signal while processing other signals

From Dev

qt5 qml c++ interaction

From Dev

QML signal QT slot with QQuickView

From Dev

Is Q_INVOKABLE needed to invoke a public QObject function from QML at all in Qt5?

From Dev

Qt C++11 lambda: Is this connect (signal-slot) correct?

From Dev

Warning when connecting c++ signal to qml slot

From Dev

Qt Connecting Signal to Slots

From Dev

Qt5: could not connect signal to slot

From Dev

QML - connect a signal from c++ to qml

From Dev

Qt-how to connect a function to a signal in qml with python? using QQmlApplicationEngine

From Dev

Wait for signal while processing other signals

From Dev

qt5 qml c++ interaction

From Dev

Connecting signal and slot not working Qt

From Dev

QT5 C++ Signal to QML Slot not working

From Dev

Fail To Connect Qml signal to C++ Slot

From Dev

Qt - Connecting C++ and QML - ReferenceError: xxx is not defined

From Dev

How to connect multiple signals to one slot in QT/QML?

Related Related

HotTag

Archive