Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

FindFiles without GUI



  • I want to load all filename-strings of a directory.
    @
    QFileSystemModel *datafolder;
    ...
    datafolder= new QFileSystemModel;
    ...
    void MainWindow::LoadFiles() {
    connect(datafolder, SIGNAL(directoryLoaded (const QString & )), this, SLOT(LoadFilesOKSlot()));
    datafolder->setFilter(QDir::AllEntries);
    datafolder->setRootPath("d:/data"); //tested with lots of different paths..
    }
    void MainWindow::LoadFilesOKSlot() { //slot invoked when QFileSystemModel finished caching the files/dirs...
    int numRows, numCols, i, j;
    QString x;
    QModelIndex ndx= datafolder->index("d:/data"); //tested with lots of different paths..
    numRows = datafolder->rowCount(ndx);
    numCols = datafolder->columnCount(ndx); // <<< breakpoint here for checking the row/col values
    for (i= 0; i < numRows; ++i) {
    for (j= 0; j < numCols; ++j) {
    x= datafolder->data(index(i,j), Qt:: DisplayRole).toString(); // <<< breakpoint here for checking strings
    }
    }
    }
    @

    Code compiles and DOES something: numCols always set to 4, numRows usually 0 or 1. When there is a valuable row, then 1st column is for name, 2nd empty, 3rd is the type, 4th is the date...that's fine

    The problem is that any directory I set by
    @QModelIndex ndx= datafolder->index("d:/data");@
    I get only 1 valuable row, that is for the root of the actual drive, instead of e.g. "D:/data" folder content.
    Sometimes I get more rows (4 and 6 rows already "happened"), but all items are empty except the first row.

    If I connect QFileSystemModel to a QTreeView, everything is fine on the screen.
    How could I get all the filenames in a given directory?



  • I suggest you use QDir class in your case.



  • QFileSystemModel uses lazy loading. I suggest you call "canFetchMore() ":http://doc.trolltech.com/stable/qabstractitemmodel.html#canFetchMore and if that returns true call "fetchMore() ":http://doc.trolltech.com/stable/qabstractitemmodel.html#fetchMore



  • Why are you using QFileSystemModel instead of QDirIterator?



  • Thanks, Denis,
    I hoped that walking through subdirs would be easier with the QFileSystemModel, but QDir + "manual handling" of sub-directories seems to be proper. I just try to understand why the QFileSystemModel approach wasn't working :)



  • [quote author="Volker" date="1295910280"]QFileSystemModel uses lazy loading. I suggest you call "canFetchMore() ":http://doc.trolltech.com/stable/qabstractitemmodel.html#canFetchMore and if that returns true call "fetchMore() ":http://doc.trolltech.com/stable/qabstractitemmodel.html#fetchMore[/quote]

    Originally after running setRootPath() I immediately called rowCount() --and got 0 or 1 row.
    When I have found in the doc's that it is in a seperate thread and directoryLoaded() signal is emitted by QFileSystemModel when reading/caching the whole filesystem is ready, I re-wrote the code into a slot function.
    It was suspicious that why is there a canFetchMore(), if slot called when all done ?



  • [quote author="novaktamas" date="1295911687"]It was suspicious that why is there a canFetchMore(), if slot called when all done ?
    [/quote]

    This function is virtual in QAbstractItemModel and is reimplemented in QFileSystemModel. It is called when a branch in the tree view is opened that is not yet cached.



  • [quote author="peppe" date="1295910898"]Why are you using QFileSystemModel instead of QDirIterator?[/quote]

    Thanks, Peppe,
    that's the winning idea by now:)) I came from the direction of Borland's c++Builder, and QDirIterator is closest to the good old FindFirst/FindNext. I started flirting with Qt Creator 2 weeks ago, and it's hard to review the so many classes. Sometimes the real one can't be found...


Log in to reply