Push StackView from C++
-
wrote on 28 Apr 2022, 12:58 last edited by therealmatiss
Hello!
I have to push QML StackView screens from different parts of C++ function - what could be the best way to do it? Why does it say here https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html that manipulating GUI from C++ is a bad idea? I literally have a GUI that's dependent from the results of my C++ program.
This is what I have so far:
QQmlApplicationEngine engine; const QUrl url(u"qrc:/Example/qml/screen_start.qml"_qs); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject* obj, const QUrl& objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); ... QObject *rootObject = engine.rootObjects().first(); QObject *qmlObject = rootObject->findChild<QObject*>("mainStackview"); const QUrl screenurl(u"qrc:/Example/qml/screen_done.qml"_qs); if (!QMetaObject::invokeMethod(qmlObject, "push", Q_ARG(QUrl, screenurl))) { SPDLOG_ERROR("Failed to push Stackview"); }
-
wrote on 28 Apr 2022, 21:29 last edited by
@therealmatiss said in Push StackView from C++:
I literally have a GUI that's dependent from the results of my C++ program.
Just like every other program does.
Learn how Qt models work. https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
-
wrote on 29 Apr 2022, 11:31 last edited by
There's literally nothing there about C++. I can't control everything from QML. I just have to know, how to send push command properly to the StackView from C++ code.
-
Hello!
I have to push QML StackView screens from different parts of C++ function - what could be the best way to do it? Why does it say here https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html that manipulating GUI from C++ is a bad idea? I literally have a GUI that's dependent from the results of my C++ program.
This is what I have so far:
QQmlApplicationEngine engine; const QUrl url(u"qrc:/Example/qml/screen_start.qml"_qs); QObject::connect( &engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject* obj, const QUrl& objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); ... QObject *rootObject = engine.rootObjects().first(); QObject *qmlObject = rootObject->findChild<QObject*>("mainStackview"); const QUrl screenurl(u"qrc:/Example/qml/screen_done.qml"_qs); if (!QMetaObject::invokeMethod(qmlObject, "push", Q_ARG(QUrl, screenurl))) { SPDLOG_ERROR("Failed to push Stackview"); }
@therealmatiss said in Push StackView from C++:
Why does it say here ... that manipulating GUI from C++ is a bad idea
What it says, is, that its a bad idea to fetch a pointer to a GUI component and than directly manipulating those elements.
What you're supposed to do is expose a c++ class/instance to QML
Either as a context property, as a singleton or as in instantiable QML component -
Emit a signal in C++, like userLogged.
in QML do something like the following :
Connections { target: YourCppObject function onUserLogged() { yourStackView.push("YourLoggedInPage.qml"); } }
-
There's literally nothing there about C++. I can't control everything from QML. I just have to know, how to send push command properly to the StackView from C++ code.
wrote on 29 Apr 2022, 15:11 last edited by@therealmatiss said in Push StackView from C++:
There's literally nothing there about C++.
https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#c-data-models
The first link (same page as this link) gives you an overview of how to present data to qml. The sublink shows how this relates to data models and shows how to expose those models to qml from c++. Having a good understanding of the model view is essential for using qml and c++ effectively together.
1/6