Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. FindFiles without GUI
QtWS25 Last Chance

FindFiles without GUI

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    novaktamas
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      I suggest you use QDir class in your case.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          Why are you using QFileSystemModel instead of QDirIterator?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • N Offline
            N Offline
            novaktamas
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0
            • N Offline
              N Offline
              novaktamas
              wrote on last edited by
              #6

              [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 ?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  novaktamas
                  wrote on last edited by
                  #8

                  [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...

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved