Qt 6.11 is out! See what's new in the release
blog
How do I Expand treeView when folder in listview is clicked.?
General and Desktop
2
Posts
1
Posters
873
Views
1
Watching
-
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 inQModelIndex QFileSystemModel::index(const QString & path, int column = 0) constSo 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.