Solved Start an QML from within an IF statement
-
Maybe the question seems trivial to many of you, but i would like to know how to start a QML only if a given condition occurs. Currently, my QML is loaded, but only if I comment on the IF statment
# code block //if ( loadSettings("live_on_monitor_1", "1").toInt() != 0 ) { master_id = loadSettings("master_id", "1"); start_url = getSportURI(loadSettings("sport_on_monitor_1", "1").toInt(), m_id); QRect primaryScreen = QGuiApplication::screens()[0]->availableGeometry(); QQmlApplicationEngine engineLive_1; qInfo() << "Monitor Enabled:" << 1; qInfo() << "Monitor StartX:" << primaryScreen.x(); qInfo() << "Monitor StartURL:" << start_url; engineLive_1.rootContext()->setContextProperty(QStringLiteral("startX"), primaryScreen.x()); engineLive_1.rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress()); engineLive_1.rootContext()->setContextProperty(QStringLiteral("start_url"), start_url); engineLive_1.load(QUrl(QLatin1String("qrc:/main.qml"))); //}
-
I forgot to add .... in debug... when execute the load statement ...
engineLive_1.load(QUrl(QLatin1String("qrc:/main.qml")));
i get stuck again in
QQmlApplicationEngine engineLive_1;
and then proceeds to execute the next code.
Also if i add the code in a function, and i try to call the function, i get the same result
bool openWindow(int numScreen, QString start_url){ QRect primaryScreen = QGuiApplication::screens()[numScreen-1]->availableGeometry(); QQmlApplicationEngine engineLive; qInfo() << "Monitor Enabled:" << numScreen; qInfo() << "Monitor StartX:" << primaryScreen.x(); qInfo() << "Monitor StartURL:" << start_url; engineLive.rootContext()->setContextProperty(QStringLiteral("startX"), primaryScreen.x()); engineLive.rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress()); engineLive.rootContext()->setContextProperty(QStringLiteral("start_url"), start_url); engineLive.load(QUrl(QLatin1String("qrc:/main.qml"))); if (engineLive.rootObjects().isEmpty()) return false; return true; }
-
@Collateral-Studios
I know absolutely nothing about QML, but with yourif
block or function yourQQmlApplicationEngine engineLive
goes out of scope and gets destroyed at the end of the block. Are you supposed to keep aQQmlApplicationEngine
in scope afterload
till no longer wanted/exited for QML functioning?? -
Exactly, actually in debug i see that the qml is opened but it disappears as soon as the end of the if cycle. It is the same thing if i run the qml from a function.
How can i keep the QML window/s open until the program is closed?
-
Hi,
Why are you using a local
QQmlApplicationEngine
? -
@SGaist
I'm not sure why, i seem to remember that other solutions have given me problems on other platforms like Android.What would be the ideal solution? I simply need to open two or more qml windows if some occur
-
Ok... Fixed ... I had to use the QQuickView that seem to work well even within an if statement or a function.
# code block bool openWindow(int numScreen, QString start_url){ QRect usedScreen = QGuiApplication::screens()[numScreen-1]->availableGeometry(); qInfo() << "Monitor Enabled:" << numScreen; qInfo() << "Monitor StartX:" << usedScreen.x(); qInfo() << "Monitor StartURL:" << start_url; QQuickView *view = new QQuickView(QUrl(QStringLiteral("qrc:/main.qml"))); view->setGeometry(usedScreen); view->rootContext()->setContextProperty(QStringLiteral("startX"), usedScreen.x()); view->rootContext()->setContextProperty(QStringLiteral("uuid"), getMacAddress()); view->rootContext()->setContextProperty(QStringLiteral("start_url"), start_url); //view_1->showFullScreen(); //if (view->rootObjects().isEmpty()) // return false; return true; }
-
You have a memory leak here. You likely want to have the
Qt::WA_DeleteOnClose
attribute set on that widget other wise it won't be deleted.