PDA

View Full Version : Display System Drives in Specific Order in QTreeView



owais_blore
4th December 2012, 07:18
I have a treeview where I am displaying all system drives using QFileSystemModel. I am looking for much better approach where a tree view can display Local Drives in one section, Removable Drive in another and so on.

Here is the code:

pSystemPrimaryModel = new QFileSystemModel(this);
/* Sets the directory that is being watched by the model to currentPath by installing
a file system watcher on it. */
pSystemPrimaryModel->setRootPath(QDir::currentPath());
pSystemPrimaryModel->setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );
// Sets the model for the view to present.
ui->PrimTreeView->setModel(pSystemPrimaryModel);
// Regard less how many columns you can do this using for:
for(int nCount = nColCount; nCount < pSystemPrimaryModel->columnCount(); nCount++)
ui->PrimTreeView->hideColumn(nCount);
It gives me the following:
8476
I want to display it like the second picture shown above. How can that be achieved?

wysota
4th December 2012, 10:34
Use a proxy model for sorting.

owais_blore
4th December 2012, 11:15
Use a proxy model for sorting.
How do we do that? It will be helpful if you can guide me :)

wysota
4th December 2012, 11:18
How do we do that? It will be helpful if you can guide me :)

QSortFilterProxyModel

owais_blore
4th December 2012, 11:28
Basically using this, i tried to play around and did something like this:

QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(pSystemPrimaryModel);

// Set the QStandardItemModel model of QTreeView to display audio and video files with details
ui->PrimTreeView->setModel(proxyModel);

it didnt work..... Looks like I have to use some functions

wysota
4th December 2012, 11:35
Since you want to change the order of the drives only, obviously just using a proxy that sorts *everything* will not work. You need to subclass it and reimplement the comparision method taking into consideration *what* you are sorting. And I have no idea why you used QStandardItemModel instead of QFileSystemModel in the last snippet. Maybe that's why it "didn't work" (whatever that means) :)

owais_blore
5th December 2012, 09:07
Well That was a typo from my side. I have used QFileSystemModel only. Looks like I have to re-implement the comparison method.


Since you want to change the order of the drives only, obviously just using a proxy that sorts *everything* will not work. You need to subclass it and reimplement the comparision method taking into consideration *what* you are sorting. And I have no idea why you used QStandardItemModel instead of QFileSystemModel in the last snippet. Maybe that's why it "didn't work" (whatever that means) :)