[SOLVED] QtQuick2ControlsApplicationViewer connect signal to slot
I want to connect a qml signal to a c++ slot via QObject::connect() in my main.cpp.
I'm understanding how to connect a signal to a slot.
Normally I need something like this: QObject::connect(viewer.rootObject(), SIGNAL(foo()), &targetClass, SLOT(foo()));
My problem is to get the rootObject of my view.
I just saw many solutions with QtQuick2ApplicationViewer. They are all using the rootObject-method.
QtQuick2ControlsApplicationViewer does not have the method rootObject().
Few weeks ago I found out that QtQuick2ControlsApplicationViewer is missing the rootContext()-method when creating a QtQuick2Controls project with the Wizard using Qt Creator 3.0.1. I just added the missing lines by myself into the .h and .cpp file.
(The bug was already reported and it was fixed in Qt Creator 3.1)
Here is the code of my main.cpp:
(I just removed some boring lines of code)
int main(int argc, char *argv)
Q_INIT_RESOURCE(resources); Storage storage; QtQuick2ControlsApplicationViewer viewer; viewer.rootContext()->setContextProperty("storage", &storage);
QObject::connect((QObject*)viewer.rootObject(), SIGNAL(getLastState()), &storage, SLOT(getLastState()));
viewer.setMainQmlFile(QStringLiteral("qml/App/main.qml")); viewer.show(); return app.exec();
Is the method rootObject() missing in my project or does it not exists?
How can I connect a qml signal with a c++ slot in my QtQuick2ControlsApplication main.cpp?
Line 18 is not working, viewer.rootObject() method does not exists for QtQuick2ControlsApplicationViewer objects.
By the way... is it possible that there is no documentation about QtQuick2ControlsApplicationViewer ??
Thanks in advance!
QtQuick2ControlsApplication is not part of Qt. It is only a convenience class that wraps a QQmlEngine and a QQuickWindow. Its code is auto-generated by Qt Creator, that's why there is no documentation. Unfortunately, as you have discovered, it is missing some important functions.
I recommend updating to Qt Creator 3.1 -- QtQuick2ControlsApplicationViewer has been removed, and is replaced with "QQmlApplicationEngine":http://qt-project.org/doc/qt-5/qqmlapplicationengine.html. You can then easily call QQmlApplicationEngine::rootObjects().
I just did what you said. Installed Qt Creator 3.1 and used the engine class. Works perfect!
I also manually transfered all data from my old projekt into a new one.
Just saw that some new files will be added to the project if you create a new project within Qt Creator 3.1. "qtquick2controlsapplicationviewer" was replaced with "deployment" folder and the qml-files(quick2) will be added to the resources folder instead of the QML folder.