PDA

View Full Version : Display a subfolder from QDirModel with QTreeView



slv1
24th February 2015, 12:25
My code below will display the whole file system. I just want to display a subfolder (ex D:\folder1), with all it's contents.
I tried setting the root index of the treeView but this will hide the folder (only the contents of the folder1 will be shown).
I also tried using "setFilterRegExp()" and "setFilterKeyColumn()", but this approach will also NOT work because it will filter the parents as well.
I also tried subclassing the QSortFilterProxy" but that seems a little too much for what I'm trying to achieve.

Is there an easier method here?


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDirModel *model = new QDirModel();
QSplitter *splitter = new QSplitter();
QTreeView *leftTree = new QTreeView(splitter);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel();

proxyModel->setSourceModel(model);
leftTree->setModel(proxyModel);

splitter->show();
return a.exec();
}

d_stranz
24th February 2015, 16:05
Probably you need to retrieve the QModelIndex for the folder you are interested in using QDirModel::index() and then set that as the root index on the tree view. I'm not sure what you hope to accomplish with a proxy model.

slv1
24th February 2015, 16:22
That works, but it will only display the contents at that path(or children in the model), I want to have the folder too, like this:

setRootIndex() will display:

---SubFolder1
---File1
---etc

I want:

-Folder1
---SubFolder1
---File1
---etc