Search code examples
qtuser-interfaceqmlqtquick2qt6

Reusing window in Qt


I want to create a desktop application using Qt Quick and QML. I understand so far how to manage widgets until I need to change the whole UI.

Example: After logging into Telegram, it loads a completely different UI with the list of your chats.

What is a better way to implement this?


Solution

  • You can use StackView for this.

    From the Qt Documentation for StackView:

     ApplicationWindow {
     title: qsTr("Hello World")
     width: 640
     height: 480
     visible: true
    
     StackView {
         id: stack
         initialItem: mainView
         anchors.fill: parent
     }
    
     Component {
         id: mainView
    
         Row {
             spacing: 10
    
             Button {
                 text: "Push"
                 onClicked: stack.push(mainView)
             }
             Button {
                 text: "Pop"
                 enabled: stack.depth > 1
                 onClicked: stack.pop()
    
             }
             Text {
                 text: stack.depth
             }
         }
     }
     }
    

    Basically you push and pop Components to the stack. You can also activate Components on the stack, so you don't have to create them everytime you need an specific item.