Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get list of all items in QML StackView that pushed into stack?



  • Hello!
    I am trying to find something about getting list of all items in QML StackView that pushed into stack. Is there any option to get it?





  • You can use this function to list all items.

    function listItems(){
    
                for(var i=0; i<stackView.children.length;i++)
                {
                    var item = stackView.get(i);
                    console.log(item.title);
                }
        }
    


  • @CKurdu Thx a lot ... Maybe you know how to avoid correctly creating duplicated object in stack? Is there in-box solution? For example if the page "settings" already existed and I don't want to create it again in stack but showing already existed. Is there kind of "singleton" for elements in the stack? Or killing existed in stack when leaving it?



  • I don't know in-box solution but you can solve your problem with the javascript file. First implement a javascript file like below

    var dict = {};
    var sv;
    function setStackView(item)
    {
        sv = item;
    }
    function push(id,item) {
        if(!dict.hasOwnProperty(id))
        {
            dict[id] = item;
            sv.push(item);
        }else{
            console.debug("dublicate!");
        }
    }
    function get(id)
    {
        return dict[id];
    }
    
    function list()
    {
        console.debug("list");
        var keys = Object.keys(dict);
        for(var i=0; i<keys.length; i++)
        {
            console.debug(keys[i]+ " : " + dict[keys[i]]);
        }
    }
    

    then you can import and use

    import "StackController.js" as StackController
    ...
    ...
       Component.onCompleted: {
                        StackController.setStackView(stackView);
                        StackController.push("page2",page2)
                        StackController.push("page1",page2)
                        StackController.push("page1",page1)
                        StackController.list();
        }
    
    
    

    But this code only works for item base not for the URL base.



  • @CKurdu Thx for reply. I am doing similar usually. I've been seeking something that in-box from pure QT without additional storing info about what in stack. Looks like nothing in it yet. First time I've been questioning about it about 4-5 years ago ...


Log in to reply