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. Know if an Item is the currentItem of a StackView
Forum Updated to NodeBB v4.3 + New Features

Know if an Item is the currentItem of a StackView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 7 Posters 3.8k Views 2 Watching
  • 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.
  • M Mark81

    Here we see a way to find the current item in a StackView checking the objectName property against the currentItem one.
    But what about if I would know this information inside the item itself?

    Example, I push several items on the stack. Each one connects to some signal from the C++ code, but I want to execute them only if the item is on the top (i.e. visible). I tried to use the visible or z properties without success.

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    @Mark81 said in Know if an Item is the currentItem of a StackView:

    but I want to execute them only if the item is on the top (i.e. visible)

    why didn't you check the docs?

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mark81
      wrote on last edited by
      #3

      I did. And I did it just again.
      Perhaps I'm blind today but from that doc I really cannot understand how an Item might now it's own position on the stack.

      JonBJ 1 Reply Last reply
      0
      • M Mark81

        I did. And I did it just again.
        Perhaps I'm blind today but from that doc I really cannot understand how an Item might now it's own position on the stack.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @Mark81
        I don't quite get what you are looking for. http://doc.qt.io/qt-5.9/qml-qtquick-controls2-stackview.html#currentItem-prop is the "current top-most item in the stack", so either your item is or is not the current item, and that's what you want to know?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mark81
          wrote on last edited by
          #5

          @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

          Example:

          main.qml

          StackView { id: stackView }
          ...
          stackView.push(item1)
          stackView.push(item2)
          stackView.push(item3)
          

          Where "itemX" are instances of MyItem:

          myitem.qml

          function doSomething()
          {
              // if (I am the top/currentItem) do something...
          }
          
          raven-worxR JonBJ J.HilkJ 3 Replies Last reply
          0
          • M Mark81

            @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

            Example:

            main.qml

            StackView { id: stackView }
            ...
            stackView.push(item1)
            stackView.push(item2)
            stackView.push(item3)
            

            Where "itemX" are instances of MyItem:

            myitem.qml

            function doSomething()
            {
                // if (I am the top/currentItem) do something...
            }
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #6

            @Mark81 said in Know if an Item is the currentItem of a StackView:

            As I said I need to know this inside each item, hence I have no access to the StackView object.

            and again ... take a look at the docs...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Mark81

              @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

              Example:

              main.qml

              StackView { id: stackView }
              ...
              stackView.push(item1)
              stackView.push(item2)
              stackView.push(item3)
              

              Where "itemX" are instances of MyItem:

              myitem.qml

              function doSomething()
              {
                  // if (I am the top/currentItem) do something...
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Mark81
              So if you are saying neither "visible" not "z" are used to tell you want you want, you must have access to the stackview (by whatever means) in order to glean your information, since I presume they are not aware they are inside a stackview.

              If that is a problem, what about sub-classing the objects you put into the stackview so that they note their parent stackview in order to search it? Or, look at http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html#supported-attached-properties, and also the whole of the Advanced Properties section there.

              KillerSmathK 1 Reply Last reply
              0
              • M Mark81

                @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

                Example:

                main.qml

                StackView { id: stackView }
                ...
                stackView.push(item1)
                stackView.push(item2)
                stackView.push(item3)
                

                Where "itemX" are instances of MyItem:

                myitem.qml

                function doSomething()
                {
                    // if (I am the top/currentItem) do something...
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #8

                @Mark81 said in Know if an Item is the currentItem of a StackView:

                @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

                Example:

                main.qml

                StackView { id: stackView }
                ...
                stackView.push(item1)
                stackView.push(item2)
                stackView.push(item3)
                

                Where "itemX" are instances of MyItem:

                myitem.qml

                function doSomething()
                {
                    // if (I am the top/currentItem) do something...
                }
                

                if you push an item it's by default the top /current item. So why don't you call the relating function when you push the item on the stackview?

                {
                   stackView.push(item1)
                   item1.doSomething()
                }
                ...
                {
                   stackView.push(item2)
                   item2.doSomething()
                }
                ....
                

                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.

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

                  @Mark81 said in Know if an Item is the currentItem of a StackView:

                  @JonB, of course the currentItem property tells me what is the top-most item. But this is a property of the StackView. As I said I need to know this inside each item, hence I have no access to the StackView object.

                  Example:

                  main.qml

                  StackView { id: stackView }
                  ...
                  stackView.push(item1)
                  stackView.push(item2)
                  stackView.push(item3)
                  

                  Where "itemX" are instances of MyItem:

                  myitem.qml

                  function doSomething()
                  {
                      // if (I am the top/currentItem) do something...
                  }
                  

                  if you push an item it's by default the top /current item. So why don't you call the relating function when you push the item on the stackview?

                  {
                     stackView.push(item1)
                     item1.doSomething()
                  }
                  ...
                  {
                     stackView.push(item2)
                     item2.doSomething()
                  }
                  ....
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @J.Hilk
                  Because he only want to do the action if the item is still the current item at the time a signal is received.

                  He could add/remove the signal handler as each item becomes/ceases to be the top-most, but that's a bit of a hassle....

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Mark81
                    So if you are saying neither "visible" not "z" are used to tell you want you want, you must have access to the stackview (by whatever means) in order to glean your information, since I presume they are not aware they are inside a stackview.

                    If that is a problem, what about sub-classing the objects you put into the stackview so that they note their parent stackview in order to search it? Or, look at http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html#supported-attached-properties, and also the whole of the Advanced Properties section there.

                    KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by KillerSmath
                    #10

                    @Mark81
                    If you need to call doSomething every time a new item comes to top of stack, you can call this function in the transition among thes items.

                    Example:

                    StackView {
                        pushEnter: Transition {
                            id: pushEnter
                            ScriptAction {
                                script: pushEnter.ViewTransition.item.dosomething();
                            }
                        }
                    
                        popEnter: Transition {
                           id: popEnter
                           ScriptAction {
                                script: pushEnter.ViewTransition.item.dosomething();
                           }
                    }
                    

                    Or when currentItem has changed then call the function

                    StackView {
                          onCurrentItemChanged: {if(currentItem) currentItem.doSomething();} // check if currentItem is not null
                    }
                    

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by GrecKo
                      #11

                      To know if your item is the current item of a StackView, you can indeed use an attached property.

                      The one you need is StackView.status

                      You might use it like so:

                      function doSomething()
                      {
                          if (StackView.status === StackView.Active)
                              doStuff();
                      }
                      

                      If yor doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item:

                      function doSomething()
                      {
                          if (someItem.StackView.status === StackView.Active)
                              doStuff();
                      }
                      
                      1 Reply Last reply
                      0
                      • DiracsbracketD Offline
                        DiracsbracketD Offline
                        Diracsbracket
                        wrote on last edited by Diracsbracket
                        #12

                        Replying to this old thread because it still "Unsolved".

                        I happened to need the exact same thing as the original poster:
                        Multiple items in my StackView had connections to the same rotary encoder button, and I naively thought that the signals would only be processed by the active item.
                        Debug messages proved otherwise, so I needed to disable the connection when the item is not active.

                        I did it as follows:

                        SomeStackViewItem {
                            Component.onCompleted: {
                                   rotConnect.enabled = Qt.binding(function() {return (StackView.status === StackView.Active)})
                            }
                        
                            Connections {
                                id: rotConnect
                                target: um
                        
                                //This does not work, since StackView is an attached property, we need to do the binding later.
                                //enabled: StackView.status === StackView.Active
                            }
                        }
                        

                        Cheers.

                        GrecKoG 1 Reply Last reply
                        0
                        • DiracsbracketD Diracsbracket

                          Replying to this old thread because it still "Unsolved".

                          I happened to need the exact same thing as the original poster:
                          Multiple items in my StackView had connections to the same rotary encoder button, and I naively thought that the signals would only be processed by the active item.
                          Debug messages proved otherwise, so I needed to disable the connection when the item is not active.

                          I did it as follows:

                          SomeStackViewItem {
                              Component.onCompleted: {
                                     rotConnect.enabled = Qt.binding(function() {return (StackView.status === StackView.Active)})
                              }
                          
                              Connections {
                                  id: rotConnect
                                  target: um
                          
                                  //This does not work, since StackView is an attached property, we need to do the binding later.
                                  //enabled: StackView.status === StackView.Active
                              }
                          }
                          

                          Cheers.

                          GrecKoG Offline
                          GrecKoG Offline
                          GrecKo
                          Qt Champions 2018
                          wrote on last edited by
                          #13

                          @Diracsbracket said in Know if an Item is the currentItem of a StackView:

                              //This does not work, since StackView is an attached property, we need to do the binding later.
                              //enabled: StackView.status === StackView.Active
                          

                          That's not why it doesn't work. I mentionned why in the post above

                          If your doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item

                          In your case, you would do :

                          SomeStackViewItem {
                              id: root
                              Connections {
                                  target: um
                                  enabled: root.StackView.status === StackView.Active
                              }
                          }
                          
                          DiracsbracketD 1 Reply Last reply
                          1
                          • GrecKoG GrecKo

                            @Diracsbracket said in Know if an Item is the currentItem of a StackView:

                                //This does not work, since StackView is an attached property, we need to do the binding later.
                                //enabled: StackView.status === StackView.Active
                            

                            That's not why it doesn't work. I mentionned why in the post above

                            If your doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item

                            In your case, you would do :

                            SomeStackViewItem {
                                id: root
                                Connections {
                                    target: um
                                    enabled: root.StackView.status === StackView.Active
                                }
                            }
                            
                            DiracsbracketD Offline
                            DiracsbracketD Offline
                            Diracsbracket
                            wrote on last edited by
                            #14

                            @GrecKo said in Know if an Item is the currentItem of a StackView:

                            That's not why it doesn't work. I mentionned why in the post above

                            You're right. That indeed simplifies things...
                            Thanks!

                            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