2
votes

I am trying to emit a signal on completion of creating an object. Like this:

Component.onCompleted: mySignal()

This is in a QML file deep in the hierarchy. I now see two solutions for accessing this signal in C++.

First is passing the signal up the hierarchy until main.qml and then in C++ do this:

//Create a Quick View object.
QQuickView *view = new QQuickView();

//Object to access QML properties and childs.
QObject *container = (QObject *) view->rootObject();

//Connect signal and slots
QObject::connect(container, SIGNAL(mySignal()), this, SLOT(onMySignal()));

This I have tried and for some reason the slot is not called. It works for all the other signals I send and emit from main.qml, but not the one emitted from Component.onComplete. I can verify that the signal is emitted from the QML side, but never received on the C++ side.

The second thing I tried was instead of passing the signal to main.qml, I would get a reference to the QML file emitting the signal I want. I tried that doing so:

//Create a Quick View object.
QQuickView *view = new QQuickView();

//Object to access QML properties and childs.
QObject *container = (QObject *) view->rootObject();

//Connect signal and slots
QObject::connect(container->findChild<QObject*>("mySignalQmlFile"), SIGNAL(mySignal()), this, SLOT(onMySignal()));

where mySignalQmlFile is the ID of the main rectangle that has the signals defined within it.

and I get the error:

QObject::connect: No such signal QQuickRectangle_QML_54::mySignal() in ..\GC\mainwindow.cpp:62

I am now not sure how to proceed.

1

1 Answers

1
votes

For your first try, I think it is possible that your signal is emitted before you even connect your signal and slot so that it is never received the signal.

For your second try, you need to use objectName from your qml file, not "mySignalQmlFile".

I recommend you to read this following tutorial.

http://developer.nokia.com/community/wiki/Using_objectName_to_find_QML_elements_from_Qt

Also, your qml file needs to be called or used before you connect signals and slot. Otherwise, it will not be able to find the object so that you would get the same error.