0

I show a QFileSystemModel through a QTreeView.

Whenever the user clicks on a directory (expanded or not expanded) I want to get a list of the files inside this directory.

void MyModel::selectionChanged(const QItemSelection& selected,const QItemSelection& deselected) {
    for (auto const & it : selected.indexes()) {
        for (int i=0;i<rowCount(it);i++) {
            auto child = it.child(i, it.column());
            qDebug() << fileName(child);
        }
    }
}

The problem with the above code is that this only seems to work once that particular directory has been expanded. As long as the directory has never been expanded (since program start) rowCount is 0.

How can I force the model to populate the children of the given model index? Without necessarily showing the children in the view? One level of children indexes would be enough in this case.

Jan Müller
  • 413
  • 3
  • 18
  • 1
    As an alternative... why not just use the path associated with a selected model index to initialize a [`QDirIterator`](http://doc.qt.io/qt-5/qdiriterator.html) and iterate over its contents? – G.M. Jan 01 '18 at 10:02
  • @G.M. well, that's a very smart suggestion! Thanks a lot. – Jan Müller Jan 01 '18 at 10:10
  • Unsure whether or not an exact reason is still of interest but can propose a solution based on overloading of http://doc.qt.io/qt-5/qabstractitemmodel.html#hasChildren – Alexander V Jan 03 '18 at 06:21

0 Answers0