Solved QTreeView with QFileSystemModel not sorting
-
Hi,
I have aQTreeView
for aQFileSystemModel
in which I only keep theName
column 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? -
QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,[=]()->void {m_treeModel->sort(0);});
-
@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... -
Coincidentally, I had the same problem as you before.
Define that:
m_model
is the object of classQFileSystemModel
, andm_rootpath
is 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 !