QTreeView with QFileSystemModel not sorting
-
wrote on 15 Feb 2018, 14:20 last edited by Diracsbracket
Hi,
I have aQTreeViewfor aQFileSystemModelin which I only keep theNamecolumn visible.I have set
m_dirTree->setSortingEnabled(true); m_dirTree->sortByColumn(index.column(), Qt::AscendingOrder);and use the following to add a subfolder to the currently selected folder:
index = m_treeModel->mkdir(index, foldername);The view is updated to show the newly added folder, but it is appended at the end of the subdir list. So even if I name my new folder AA, it will still show after ZZZ.
Forcing a sort by clicking on the header's sort arrow does not correct this either?
What is going on? -
wrote on 15 Feb 2018, 15:02 last edited by
QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,[=]()->void {m_treeModel->sort(0);}); -
QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,[=]()->void {m_treeModel->sort(0);});wrote on 16 Feb 2018, 06:55 last edited by Diracsbracket@VRonin said in QTreeView with QFileSystemModel not sorting:
QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,=->void {m_treeModel->sort(0);});
Thanks @VRonin .
I tried you connect, and the slot is called, but the sort has no effect.
The thing is, when I create the AA folder manually in Explorer, the tree updates correctly and shows AA at the top. So maybe I should try using another method to make the directory than
QFileSysModel::mkdir()Indeed, when I use
QDir::mkdirI()the model updates to the correct sort order... -
wrote on 9 Feb 2022, 15:32 last edited by
Coincidentally, I had the same problem as you before.
Define that:
m_modelis the object of classQFileSystemModel, andm_rootpathis the root path of your filetree.In the constructor function of QTreeView, there should be the following code:
m_model->setRootPath(m_rootpath); setModel(m_model); setRootIndex(m_model->index(m_rootpath));Add the following code where you have done the file operation.
m_model->setRootPath(""); m_model->setRootPath(m_rootpath);The method WORKS in my project !