Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I Expand treeView when folder in listview is clicked.?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 735 Views
  • 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.
  • S Offline
    S Offline
    SwePtr
    wrote on last edited by
    #1

    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));
      }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SwePtr
      wrote on last edited by
      #2

      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.

      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