QT arranging Components in the StackView

1k views Asked by At

We are developing QT cross platform application. Application has many functional Screens and navigations. For simplicity of handling screens we are using QT QML StackView. Each item pushed to StackView is Page. I am using following function to push and pop screens.

stackview.push(someurl); //push new url

stackview.pop(); //pop current URL or page

My Question is how can we reuse Components/Pages already pushed on to the stack?

Example : I have following screens A, B, C, D. stack is looking like A->B->C->D. Now from screen D i want to navigate to Screen A. Due to uniform architecture of the application i don't want to do 3 times pop(), instead application should reuse already pushed Screen A and bring it to top.

We can simply push the screen A to StackView (A->B->C->D->A), but we are connecting different Signal/Slot to/from each screen and it will not be good practice to connect every time screen is pushed, also don't want to grow the application StackView size.

What will be the best approach to use in this case ? Any other QT Component/Control which i can use in this case so that i can render Pages depending on Index?

We tried creating component in advance as qml Property and push it depending on URL. It is useful worked great in sample application. but in our application QML component data is closely dependent on C++ data( or some calculations at run time). Application is causing crash if we create component in advance. So we are not willing to use that approach.

property Page page1: Screen1{} //Screen1.qml 
property Page page2: Screen2{} //Screen2.qml 
property Page page3: Screen3{} //Screen3.qml 
property Page page4: Screen4{} //Screen4.qml 

Is it possible to laod Stackview component based on Index, i am not finding anything related to that in documents.

1

There are 1 answers

0
Blabdouze On

Due to uniform architecture of the application i don't want to do 3 times pop(), instead application should reuse already pushed Screen A and bring it to top.

You don't have to call pop() three times to get back to A. According to the documentation :

Item pop(Item item = undefined) [...]

item: if specified, all items down to (but not including) item will be popped off. If item is null, all items down to (but not including) the first item will be popped. If not specified, only the current item will be popped.

Only call pop(null) once to have your stack going to the first A pushed.

An other possible approach is to use StackLayout that allow you to display QML Components based on an index.

StackLayout {
    id: layout

    Screen1 {}
    Screen2 {}
    Screen3 {}
    Screen4 {}
}

Then you can select which screen appears by changing the currentIndex property : layout.currentIndex = 1 // display Screen2