Yes, I tried with QStandardItemModel and 3 levels of QStandardItem objects and it all works as expected:
#include <QApplication>
#include <QHeaderView>
#include <QMessageBox>
#include <QModelIndex>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QTreeView>
int main(int argc, char *argv[])
{
const int nbOfItems = 10;
const int nbOfSubItems = 10;
const int nbOfSubSubItems = 10;
QStandardItem subSubItem
[nbOfItems
][nbOfSubItems
][nbOfSubSubItems
];
for (int i = 0; i < nbOfItems; ++i) {
item
[i
].
setText(QString("%1").
arg(i
+1));
for (int j = 0; j < nbOfSubItems; ++j) {
subItem
[i
][j
].
setText(QString("%1").
arg(j
+1));
for (int k = 0; k < nbOfSubSubItems; ++k) {
subSubItem
[i
][j
][k
].
setText(QString("%1").
arg(k
+1));
subItem[i][j].setChild(k, &subSubItem[i][j][k]);
}
item[i].setChild(j, &subItem[i][j]);
}
sim.setItem(i, &item[i]);
}
w.header()->hide();
w.setModel(&sim);
w.show();
QModelIndex mi
= sim.
indexFromItem(&subSubItem
[7-1][6-1][9-1]);
w.setCurrentIndex(mi);
w.scrollTo(mi);
return a.exec();
}
#include <QApplication>
#include <QHeaderView>
#include <QMessageBox>
#include <QModelIndex>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QTreeView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel sim;
const int nbOfItems = 10;
const int nbOfSubItems = 10;
const int nbOfSubSubItems = 10;
QStandardItem item[nbOfItems];
QStandardItem subItem[nbOfItems][nbOfSubItems];
QStandardItem subSubItem[nbOfItems][nbOfSubItems][nbOfSubSubItems];
for (int i = 0; i < nbOfItems; ++i) {
item[i].setText(QString("%1").arg(i+1));
for (int j = 0; j < nbOfSubItems; ++j) {
subItem[i][j].setText(QString("%1").arg(j+1));
for (int k = 0; k < nbOfSubSubItems; ++k) {
subSubItem[i][j][k].setText(QString("%1").arg(k+1));
subItem[i][j].setChild(k, &subSubItem[i][j][k]);
}
item[i].setChild(j, &subItem[i][j]);
}
sim.setItem(i, &item[i]);
}
QTreeView w;
w.header()->hide();
w.setModel(&sim);
w.show();
QModelIndex mi = sim.indexFromItem(&subSubItem[7-1][6-1][9-1]);
w.setCurrentIndex(mi);
w.scrollTo(mi);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
which is clearly good, but very frustrating too, since I can't understand why it doesn't work the same with QFileSystemModel.
---------------------------------------
Finally got it!
Thanks wysota for asking me to check with another model. The fact that it worked with another model confirmed to me that my current use of QFileSystemModel was probably not optimal.
So, after going through the QFileSystemModel documentation in a bit more detail (until now, I had assumed that the 'problem' was with QTreeView and had therefore kind of 'ignored' QFileSystemModel until now), I noticed the directoryLoaded(QString) signal. So, I have quickly tested that in my application and, now, everything works as expected. So, yes, RTFM, but read the right section!
Bookmarks