[SOLVED] StackView: check what the currentItem is?



  • Hi,

    I have a StackView with 2 pages: the initial one and the login page, which is pushed to the StackView when the user wants to login.
    In some cases I want to push the login page to the stack only if it's not already the current item.
    The code is like this
    @
    Component {
    id: mainPage
    MyMainPage { }
    }
    Component {
    id: loginPage
    MyLoginPage { }
    }

    StackView {
    id: stack
    }

    // Something else
    MyEngine {
    onMySignal: {
    if (stack.currentItem !== loginPage)
    stack.push(loginPage);
    }
    }
    @

    Problem: even though the currentItem is the loginPage, it is pushed once again. What exactly does happen when I run the comparison stack.currentItem !== loginPage? I tried to debug and I noticed that currentItem contains properties of the MyLoginPage qml component... so what's the problem?



  • You try with following. Don't just insert Item. Have some properties as follows.

    @ mystack.push({item:login,properties:{objectName:"login"}});

            if (mystack.currentItem.objectName != "login" ){
                console.log("Add Login ="+mystack.depth)
                mystack.push({item:login,properties:{objectName:"login"}});
            }else{
                console.log("It is not login page")
            }
            var it = mystack.currentItem
            console.log("ObjectName="+it.objectName)
        }@


  • Thank you, that works. I also learned that objectName must be a property already declared in the qml file of the page I want to push, it won't be attached at runtime by the push function. Not a problem, since declaring, in each page, a string property with the page name makes perfect sense.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.