How do I Expand treeView when folder in listview is clicked.?



  • Hi, I have two models. treeViewmodel and listViewmodel.
    To show folder in listView when click on treeView is simple.

     void Dialog::on_treeView_clicked(QModelIndex index){
        QString pathTmp;
        pathTmp = treeViewModel->fileInfo(index).absoluteFilePath();
        ui->listView->setRootIndex(listViewModel->setRootPath(pathTmp));
    }
    

    How do I expand the treeview from where folder in listview is clicked.?
    The listview function right now is.

     void Dialog::on_listView_clicked(QModelIndex index){
        QString pathTmp;
        pathTmp=listViewModel->fileInfo(index).absoluteFilePath();
        ui->listView->setRootIndex(listViewModel->setRootPath(pathTmp));
      }


  • Ok, I figured it out.
    I searched for "qt create index from path" and found that the soultion was in

    QModelIndex QFileSystemModel::index(const QString & path, int column = 0) const
    

    So here is the final code.

    void Dialog::on_listView_clicked(QModelIndex index){
        QString pathTmp = listViewModel->fileInfo(index).absoluteFilePath();
        ui->listView->setRootIndex(listViewModel->setRootPath(pathTmp));
       QModelIndex  tv_index = treeViewModel->QFileSystemModel::index (pathTmp);
        ExpandToIndex (tv_index);
    }
    
    void Dialog::ExpandToIndex(QModelIndex index){
        while(index.isValid()){
            ui->treeView->expand(index);
            index = index.parent();
        }
    }
    

    Maybe not the best solution - but it works for now.


Log in to reply
 

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