Solved Setting Up StackView's initialItem property from c++.
-
I want to set up the initialItem of StackView from c++,
This is my code
main.cpp
@
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QObject *rootObject = engine.rootObjects().first();
QObject stack = rootObject->findChild<QObject>("pageStack");
if(stack)
{
stack->setProperty("initialItem" , "qrc:/newPage.qml");
}
qDebug() << "initialItem :" << stack->property("initialItem").toString(); **/prints output as - > initialItem : "qrc:/newPage.qml" /
if(engine.rootObjects().isEmpty())
return -1;return app.exec();
}
@
main.qml
@
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3ApplicationWindow {
id: mainWindow
visible: true
width: 1024
height: 768Rectangle { color: "#ffffff" anchors.fill: parent objectName: "rect" } StackView { id: stackView objectName: "pageStack" anchors.fill: parent }
}
@The property is getting set (from the debug message) but it is not moving to that specified url. Please help understand what i am doing wrong. Thanks in advance
-
@Gurubaran
I know nothing about QML, but your property name has space-colon on the end of it:stack->setProperty("initialItem :" , "qrc:/newPage.qml");
I would have thought it should be just
initialItem
, as in yourstack->property("initialItem").toString()
, no? -
I moved the content of main.qml to MainWindow.qml and implemented a Loader qml type with a Timer qml type (to give a small delay before loading the first qml page) on main.qml, this solved the problem.
If there is any better Solution please let me know .
Thank You. -
@Gurubaran
Consider:Component.onCompleted: { stackView.push("newPage.qml") }
-
@Tom_H said in Setting Up StackView's initialItem property from c++.:
@Gurubaran
Consider:Component.onCompleted: { stackView.push("newPage.qml") }
That's a more verbose way to write
initialItem: "newPage.qml"
. -
@GrecKo
Yes, I didn't understand why he wanted to do it from C++ or then use a timer. But of course you are right.