PDA

View Full Version : QDirModel, QTreeView, and QListView



gimmejimmy
24th June 2009, 07:19
Hi, I am trying to create a tree view and a list view. The tree view should display only directories, and the list view should display only files. Clicking on a directory in the tree view should change the index of the list view. I tried this, having separate QDirModels, one each for the tree and list views:

QDirModel dirModel;
QDirModel fileModel;
QTreeView tree(&window);
QListView list(&window);
tree.setModel(&dirModel);
list.setModel(&fileModel);
QObject::connect(&tree, SIGNAL(clicked(const QModelIndex &)),
&list, SLOT(setRootIndex(const QModelIndex &)));
but the signals don't connect properly, ie, clicking on the tree view makes the list view go blank.
I also tried this, having a single QDirModel for both views:

QDirModel model;
QTreeView tree(&window);
QListView list(&window);
model.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
tree.setModel(&model);
list.setModel(&model);
QObject::connect(&tree, SIGNAL(clicked()),
&list, SLOT(setCurrentIndex()));
The signal and slot here works, clicking the tree view changes the list view index, however only directories are displayed in both views.
Hints? Thanks!

nish
24th June 2009, 07:29
the slot setRootIndex aspects the QModelIndex of the same view. but u are passing the modelIndex of tree into the list. so the setRootIndex treat it as an invalid index.

gimmejimmy
24th June 2009, 07:41
So it's not possible to use separate QDirModels for tree and list and connect them? Can I use an intermediary modelIndex?
Or if I use a single QDirModel for both of them can I have separate filters for each view?