Dynamic QML Page loader
-
Thanks @ekkescorner !!! Your blog seems to be very interesting for my purposes. Anyway, i will try to answer either to @shaan7 . I would like to have something that doesn't need to know how the pattern is made. Let me try to explain better ( sorry but my english isn't so rich!).
Each "page" must contain a Loader object for loading another source. In this way, going back and forward becomes only a call to Loader.setsource("next_page.qml"). The only problem is that i am not still able to close the "current" page as it loads a new one. Doing this, i will have only one QML file loaded at time, reducing the resources used by my interface. I have done this long time ago using QWidget, and i would like to do the same using QML .Any hint will be really appreciate!!!
Thanks a lot
-
Ah, so you don't want
StackView
-based navigation per se. In that case, lets say you first callLoader.setSource("Page1.qml")
, theLoader
will load it and display it. Now, when you want to loadPage2
, I understand that you want to unloadPage1
to conserve resources. To do that, you can simply callLoader.setSource("Page2.qml")
and it will do exactly that - it will unloadPage1
and loadPage2
. -
Why not just use
StackView
and callreplace
when you want to change the page ? -
Ok @shaan7 !!! You are suggesting me to use one single Loader, and then each QML file that will be loaded must call it for changing the content of the page, right?
In this way i can't separate the the pages from the Loader, and i would like to separate each page from any other component as much as possible. -
@CristianoNarcisiVidex said in Dynamic QML Page loader:
In this way i can't separate the the pages from the Loader
Um, I'm not sure I understand what kind of separation you want here. No matter how you implement things, if you need some code in
Page1
to navigate toPage2
, then that code will have to callsomefunc("Page2.qml")
. When you're using a globalLoader
, its just thatsomefunc
sets theLoader
'sactive
property accordingly.As an example, lets say I call my
somefunc
asnavigate
:main.qml
:Window { width: 800 height: 600 function navigate(page) { loader.source = page + ".qml"; } Loader { id: loader anchors.fill: parent source: "Page1.qml" } }
Page1.qml
Item { id: page1 Button { text: "Next" onClicked: navigate("Page2") } }
Page2.qml
... as required ...In a different approach, I can use a
StackView
instead ofLoader
where thenavigate
will callreplace
as @GrecKo described. -
Thanks a lot @shaan7! We are about to reach a good solution. I am trying to do the same, but i would like to use Window instead of Item in QML pages. Changing this , the application gets closed after calling the setSource methods.
Is it the same to you?Thanks!
-
@CristianoNarcisiVidex said in Dynamic QML Page loader:
but i would like to use Window instead of Item in QML pages
Why would you do that? It will mean that every time user navigates to new page, a new
Window
will be shown, it might not be a good experience.About why the application gets closed, can you share a code snippet? Or even a link to a git repo works.
-
here is the main.qml, where i have the loader
Window { width: 640 height: 480 function navigate(page) { loader.setSource(page); } Loader { id: loader anchors.fill: parent source: "FirstItem.qml" } }
This is FirstItem.qml
Window{ visible: true width: 800 height: 900 color: "green" Text { id: element x: 206 y: 234 text: qsTr("First Element") horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.bold: true anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter font.pixelSize: 43 } MouseArea { anchors.fill: parent onClicked: { navigate("SecondItem.qml") } } }
And this the second
Window{ visible: true width: 640 height: 480 color: "white" Text { id: element x: 206 y: 234 text: qsTr("Second Element") horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.bold: true anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter font.pixelSize: 43 } MouseArea { anchors.fill: parent onClicked: { navigate("FirstItem.qml") } } }
-
Ah ok, the app exits because the moment your first
Window
closes (before loading the second), your app has no visibleWindow
s. By default, Qt apps will exit when that happens. You can disable this behavior by addingapp.setQuitOnLastWindowClosed(false);
inmain.cpp
-
@shaan7 said in Dynamic QML Page loader:
Wonderful!!! You deserve more than one like!!! Thank's a lot @shaan7 !!!