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

QML TreeView expand() method not working



  • Hi there !

    I am currently using a QML TreeView based on a c++ model that is built when the user select some data to be showed.
    It is working correctly and I can expand or collapse it myself by clicking on the button.

    I would like it to be fully expanded every time it is charged but I can't get the expand() method to work.
    I tried with this line but without success :

    myTreeView.expand(treeView.rooIndex);
    

    QUESTION : Do I need to implement something in my cpp model for it to work ? Or maybe I just missed something else ?

    ps : It might be related to the TreeModel::index() method of the model which I was not sure how to implement.



  • Hi Buck,

    I was facing the same issue. I "solved" it by using this loop:

    function expandAll() {
        for(var i=0; i < myModel.rowCount(); i++) {
            var index = myModel.index(i,0)
            if(!myTreeView.isExpanded(index)) {
                myTreeView.expand(index)
            }
        }
    }
    

    But it's delicate, because you can't call it while filling the model. Only after it is done. Otherwise the TreeView won't work/look properly. For me TreeView is only a have done Control. I'm missing an attribute in TreeView "alwaysExpanded" or something.

    Hope this helps!
    Best,
    Nexu


Log in to reply