Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why this stackview.pop not working?
QtWS25 Last Chance

Why this stackview.pop not working?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 5 Posters 995 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by
    #1

    in my program stackview.push() is working but stackview.pop() not working, I tried stackview.pop(null) and stackview.pop(["qrc:/main.qml"]) but still not working

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    import QtQuick.Window 2.0
    import QtGraphicalEffects 1.12
    import QtQuick.Controls.Universal 2.12
    import QtQuick.Layouts 1.12
    
    Page {
    
        width: Screen.width
        height: Screen.height
        visible: true
        Rectangle {
            x: -10
            y: 20
            width: 1324
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 70
            anchors.topMargin: 0
            height: 336
            radius: 25
            border.width: 5
            border.color: "green"
            Column {
                anchors.fill: parent
                anchors.rightMargin: 22
                anchors.leftMargin: 22
                anchors.topMargin: 14
                anchors.bottomMargin: 24
                Row {
                    y: 100
                    height: 65
                    anchors.left: parent.left
                    anchors.right: parent.right
    
                    Rectangle {
                        y: 100
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom
                        anchors.horizontalCenter: parent.horizontalCenter
                        width:  0.95 * parent.width
                        height: 55
                        radius: 15
                        border.width: 2
                        border.color: "red"
                        color: "white"
    
                        Label {
                            id: label2
                            x: 195
                            y: 19
                            width: 67
                            height: 27
                            text: qsTr("Name:")
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pointSize: 12
                        }
                    }
                }
                Row {
                    anchors.left: parent.left
                    anchors.right: parent.right
                    height: 0.1 * parent.height
                    Rectangle {
                        width: 1262
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom
                        anchors.horizontalCenter: parent.horizontalCenter
                        height: 0.01 * parent.height
                        Image {
                            id: img
                        }
    
                        Timer {
                            interval:100; running: true; repeat: true
                            onTriggered: img.source = "image://imgProvider/" + new Date().getTime();
                        }
                    }
                }
            }
    
            Button {
                id: button
                x: 613
                y: 628
                text: qsTr("Record")
                checkable: true
                font.pointSize: 12
                font.family: "Arial"
                background: Rectangle {
                    id: rect
                    radius: 20
                    property bool isRed: true
                    color: isRed? "red" : "green"
                }
                onClicked:{
                    rect.isRed = false
                    timer.start()
                }
            }
    
            Timer{
                id: timer
                interval:15000; running: false; repeat: false
                onTriggered:   img.grabToImage(function(result)
                {
                    stackView.push( "qrc:/ThirdPage.qml",  {"url" : result.url} )
                    rect.isRed = true
                }, Qt.size(img.width, img.height));
            }
    
            Button {
                id: button1
                x: 296
                y: 628
                width: 100
                height: 30
                checkable: true
                text: qsTr("Back")
                font.pointSize: 12
                font.family: "Arial"
                onClicked: stackView.pop()
                background: Rectangle {
                    border.color: "gray"
                    color: "white"
                }
            }
        }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • zhmhZ zhmh

      @J-Hilk I do not fully understand what you mean. Can you give an example?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #8

      @zhmh
      a bit clustered, so it fits all in one qml file. I assume your Pages are separated qml files, should work the same way.

      import QtQuick 2.12
      import QtQuick.Controls 2.12
      import QtQuick.Window 2.12
      import QtQuick.Layouts 1.12
      
      Window {
          visible: true
          width: 500
          height: 500
          title: qsTr("Hello World")
      
      
          StackView{
              id:myStackView
      
              anchors.fill: parent
      
              initialItem: Column{
                  Button{
                      width: myStackView.width
                      height:  50
                      text: qsTr("Page1")
                      onClicked: myStackView.push(page1)
                  }
      
                  Button{
                      width: myStackView.width
                      height:  50
                      text: qsTr("Page2")
                      onClicked: myStackView.push(page2)
                  }
              }
      
      
              onCurrentItemChanged: if(myStackView.currentItem.popView)myStackView.currentItem.popView.connect(myStackView.pop);
      
          }
      
          Component{
              id: page1
              Page{
                  id:p1
                  signal popView()
                  onPopView: console.log("button on page 1 clicked")
                  Button {
                      anchors.bottom: parent.bottom
                      text: qsTr("Page 2")
                      onClicked: p1.popView()
                  }
              }
          }
      
          Component{
              id:page2
              Page{
                  id:p2
                  signal popView()
                  onPopView: console.log("button on page 2 clicked")
                  Button {
                      anchors.bottom: parent.bottom
                      text: qsTr("Page 1")
                      onClicked: p2.popView()
                  }
              }
          }
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      zhmhZ 1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        It should work. Are you trying to pop before you push ? Did you confirm that you are inserting the element for sure ?

        Please look at the documentation. It has working example. Comparing with the example may help.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • zhmhZ zhmh

          in my program stackview.push() is working but stackview.pop() not working, I tried stackview.pop(null) and stackview.pop(["qrc:/main.qml"]) but still not working

          import QtQuick 2.12
          import QtQuick.Controls 2.5
          import QtQuick.Window 2.0
          import QtGraphicalEffects 1.12
          import QtQuick.Controls.Universal 2.12
          import QtQuick.Layouts 1.12
          
          Page {
          
              width: Screen.width
              height: Screen.height
              visible: true
              Rectangle {
                  x: -10
                  y: 20
                  width: 1324
                  anchors.top: parent.top
                  anchors.bottom: parent.bottom
                  anchors.bottomMargin: 70
                  anchors.topMargin: 0
                  height: 336
                  radius: 25
                  border.width: 5
                  border.color: "green"
                  Column {
                      anchors.fill: parent
                      anchors.rightMargin: 22
                      anchors.leftMargin: 22
                      anchors.topMargin: 14
                      anchors.bottomMargin: 24
                      Row {
                          y: 100
                          height: 65
                          anchors.left: parent.left
                          anchors.right: parent.right
          
                          Rectangle {
                              y: 100
                              anchors.top: parent.top
                              anchors.bottom: parent.bottom
                              anchors.horizontalCenter: parent.horizontalCenter
                              width:  0.95 * parent.width
                              height: 55
                              radius: 15
                              border.width: 2
                              border.color: "red"
                              color: "white"
          
                              Label {
                                  id: label2
                                  x: 195
                                  y: 19
                                  width: 67
                                  height: 27
                                  text: qsTr("Name:")
                                  horizontalAlignment: Text.AlignHCenter
                                  verticalAlignment: Text.AlignVCenter
                                  font.pointSize: 12
                              }
                          }
                      }
                      Row {
                          anchors.left: parent.left
                          anchors.right: parent.right
                          height: 0.1 * parent.height
                          Rectangle {
                              width: 1262
                              anchors.top: parent.top
                              anchors.bottom: parent.bottom
                              anchors.horizontalCenter: parent.horizontalCenter
                              height: 0.01 * parent.height
                              Image {
                                  id: img
                              }
          
                              Timer {
                                  interval:100; running: true; repeat: true
                                  onTriggered: img.source = "image://imgProvider/" + new Date().getTime();
                              }
                          }
                      }
                  }
          
                  Button {
                      id: button
                      x: 613
                      y: 628
                      text: qsTr("Record")
                      checkable: true
                      font.pointSize: 12
                      font.family: "Arial"
                      background: Rectangle {
                          id: rect
                          radius: 20
                          property bool isRed: true
                          color: isRed? "red" : "green"
                      }
                      onClicked:{
                          rect.isRed = false
                          timer.start()
                      }
                  }
          
                  Timer{
                      id: timer
                      interval:15000; running: false; repeat: false
                      onTriggered:   img.grabToImage(function(result)
                      {
                          stackView.push( "qrc:/ThirdPage.qml",  {"url" : result.url} )
                          rect.isRed = true
                      }, Qt.size(img.width, img.height));
                  }
          
                  Button {
                      id: button1
                      x: 296
                      y: 628
                      width: 100
                      height: 30
                      checkable: true
                      text: qsTr("Back")
                      font.pointSize: 12
                      font.family: "Arial"
                      onClicked: stackView.pop()
                      background: Rectangle {
                          border.color: "gray"
                          color: "white"
                      }
                  }
              }
          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @zhmh I fail to see you actual stackview / pop/push calls in the code you posted, so I can only guess.

          But, you can't pop() the initial item, if you have set that.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          zhmhZ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @zhmh I fail to see you actual stackview / pop/push calls in the code you posted, so I can only guess.

            But, you can't pop() the initial item, if you have set that.

            zhmhZ Offline
            zhmhZ Offline
            zhmh
            wrote on last edited by
            #4

            @J-Hilk in button with id:button1 I used the pop (last lines) and in id: timer I used push()
            I want to pop to main.qml which is my first page with ApplicationWindow{}
            in main.qml where stackview is defined:

            main.qml:

            property alias stackView: stackView
            StackView {
                    id: stackView
                    initialItem: main
                    anchors.fill: parent
                    anchors.rightMargin: -28
                    anchors.bottomMargin: -19
                    anchors.leftMargin: 28
                    anchors.topMargin: 19
            
                   //some code
            }
            
            KroMignonK J.HilkJ 2 Replies Last reply
            0
            • zhmhZ zhmh

              @J-Hilk in button with id:button1 I used the pop (last lines) and in id: timer I used push()
              I want to pop to main.qml which is my first page with ApplicationWindow{}
              in main.qml where stackview is defined:

              main.qml:

              property alias stackView: stackView
              StackView {
                      id: stackView
                      initialItem: main
                      anchors.fill: parent
                      anchors.rightMargin: -28
                      anchors.bottomMargin: -19
                      anchors.leftMargin: 28
                      anchors.topMargin: 19
              
                     //some code
              }
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @zhmh said in Why this stackview.pop not working?:

              property alias stackView: stackView
              StackView {
                   id: stackView
              

              This is very ugly and should ends with a binding loop issue.
              I would change this into:

              property alias stackView: _stackView
              StackView {
                  id: _stackView
              ...
              

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              GrecKoG 1 Reply Last reply
              2
              • zhmhZ zhmh

                @J-Hilk in button with id:button1 I used the pop (last lines) and in id: timer I used push()
                I want to pop to main.qml which is my first page with ApplicationWindow{}
                in main.qml where stackview is defined:

                main.qml:

                property alias stackView: stackView
                StackView {
                        id: stackView
                        initialItem: main
                        anchors.fill: parent
                        anchors.rightMargin: -28
                        anchors.bottomMargin: -19
                        anchors.leftMargin: 28
                        anchors.topMargin: 19
                
                       //some code
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @zhmh thats definitely the wrong approach, you shouldn't rely on global/recursive identifier lookup. Thats bound to break really soon.

                give your page component pop and push signals, call those from the buttons and connect the signals in the qml file, where your Page and StackWindow instance is.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                zhmhZ 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @zhmh thats definitely the wrong approach, you shouldn't rely on global/recursive identifier lookup. Thats bound to break really soon.

                  give your page component pop and push signals, call those from the buttons and connect the signals in the qml file, where your Page and StackWindow instance is.

                  zhmhZ Offline
                  zhmhZ Offline
                  zhmh
                  wrote on last edited by
                  #7

                  @J-Hilk I do not fully understand what you mean. Can you give an example?

                  J.HilkJ 1 Reply Last reply
                  0
                  • zhmhZ zhmh

                    @J-Hilk I do not fully understand what you mean. Can you give an example?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #8

                    @zhmh
                    a bit clustered, so it fits all in one qml file. I assume your Pages are separated qml files, should work the same way.

                    import QtQuick 2.12
                    import QtQuick.Controls 2.12
                    import QtQuick.Window 2.12
                    import QtQuick.Layouts 1.12
                    
                    Window {
                        visible: true
                        width: 500
                        height: 500
                        title: qsTr("Hello World")
                    
                    
                        StackView{
                            id:myStackView
                    
                            anchors.fill: parent
                    
                            initialItem: Column{
                                Button{
                                    width: myStackView.width
                                    height:  50
                                    text: qsTr("Page1")
                                    onClicked: myStackView.push(page1)
                                }
                    
                                Button{
                                    width: myStackView.width
                                    height:  50
                                    text: qsTr("Page2")
                                    onClicked: myStackView.push(page2)
                                }
                            }
                    
                    
                            onCurrentItemChanged: if(myStackView.currentItem.popView)myStackView.currentItem.popView.connect(myStackView.pop);
                    
                        }
                    
                        Component{
                            id: page1
                            Page{
                                id:p1
                                signal popView()
                                onPopView: console.log("button on page 1 clicked")
                                Button {
                                    anchors.bottom: parent.bottom
                                    text: qsTr("Page 2")
                                    onClicked: p1.popView()
                                }
                            }
                        }
                    
                        Component{
                            id:page2
                            Page{
                                id:p2
                                signal popView()
                                onPopView: console.log("button on page 2 clicked")
                                Button {
                                    anchors.bottom: parent.bottom
                                    text: qsTr("Page 1")
                                    onClicked: p2.popView()
                                }
                            }
                        }
                    }
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    zhmhZ 1 Reply Last reply
                    1
                    • KroMignonK KroMignon

                      @zhmh said in Why this stackview.pop not working?:

                      property alias stackView: stackView
                      StackView {
                           id: stackView
                      

                      This is very ugly and should ends with a binding loop issue.
                      I would change this into:

                      property alias stackView: _stackView
                      StackView {
                          id: _stackView
                      ...
                      
                      GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #9

                      @KroMignon no, this is fine. https://doc.qt.io/qt-6/qtqml-syntax-objectattributes.html#considerations-for-property-aliases.

                      KroMignonK 1 Reply Last reply
                      2
                      • J.HilkJ J.Hilk

                        @zhmh
                        a bit clustered, so it fits all in one qml file. I assume your Pages are separated qml files, should work the same way.

                        import QtQuick 2.12
                        import QtQuick.Controls 2.12
                        import QtQuick.Window 2.12
                        import QtQuick.Layouts 1.12
                        
                        Window {
                            visible: true
                            width: 500
                            height: 500
                            title: qsTr("Hello World")
                        
                        
                            StackView{
                                id:myStackView
                        
                                anchors.fill: parent
                        
                                initialItem: Column{
                                    Button{
                                        width: myStackView.width
                                        height:  50
                                        text: qsTr("Page1")
                                        onClicked: myStackView.push(page1)
                                    }
                        
                                    Button{
                                        width: myStackView.width
                                        height:  50
                                        text: qsTr("Page2")
                                        onClicked: myStackView.push(page2)
                                    }
                                }
                        
                        
                                onCurrentItemChanged: if(myStackView.currentItem.popView)myStackView.currentItem.popView.connect(myStackView.pop);
                        
                            }
                        
                            Component{
                                id: page1
                                Page{
                                    id:p1
                                    signal popView()
                                    onPopView: console.log("button on page 1 clicked")
                                    Button {
                                        anchors.bottom: parent.bottom
                                        text: qsTr("Page 2")
                                        onClicked: p1.popView()
                                    }
                                }
                            }
                        
                            Component{
                                id:page2
                                Page{
                                    id:p2
                                    signal popView()
                                    onPopView: console.log("button on page 2 clicked")
                                    Button {
                                        anchors.bottom: parent.bottom
                                        text: qsTr("Page 1")
                                        onClicked: p2.popView()
                                    }
                                }
                            }
                        }
                        
                        zhmhZ Offline
                        zhmhZ Offline
                        zhmh
                        wrote on last edited by
                        #10

                        @J-Hilk Thank you when I defining the initialItem correctly
                        , pop worked

                        1 Reply Last reply
                        1
                        • GrecKoG GrecKo

                          @KroMignon no, this is fine. https://doc.qt.io/qt-6/qtqml-syntax-objectattributes.html#considerations-for-property-aliases.

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #11

                          @GrecKo said in Why this stackview.pop not working?:

                          no, this is fine. https://doc.qt.io/qt-6/qtqml-syntax-objectattributes.html#considerations-for-property-aliases.

                          Thanks for the update!
                          Didn't know this.
                          I am always a little bit paranoiac about possible binding loop issues, they are never easy to find / resolve.

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved