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. QFileSystemModel::rowCount return 0
Forum Updated to NodeBB v4.3 + New Features

QFileSystemModel::rowCount return 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 593 Views 1 Watching
  • 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by
    #1
    QFileSystemModel *model = new QFileSystemModel;
    QString initPath("/");
    model->setRootPath(initPath);
    QModelIndex parentIndex = model->index(initPath);
    bool ok = parentIndex.isValid();
    if (ok)
    {
        int numRows = model->rowCount(parentIndex);
        // numRows == 0, why?
        int numCols = model->columnCount(parentIndex);
        // numCols == 4, as expected
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      You have to wait for QFileSystemModel::directoryLoaded()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      jronaldJ 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        You have to wait for QFileSystemModel::directoryLoaded()

        jronaldJ Offline
        jronaldJ Offline
        jronald
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QFileSystemModel::rowCount return 0:

        You have to wait for QFileSystemModel::directoyLoaded()

        OK, however in the doc Model/View Programming:

        QFileSystemModel *model = new QFileSystemModel;
        QModelIndex parentIndex = model->index(QDir::currentPath());
        int numRows = model->rowCount(parentIndex);
        

        PS: to locate the code, search numRows, the first one.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'll take a look at the docs later this day to see if it's correct.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • jronaldJ jronald

            @Christian-Ehrlicher said in QFileSystemModel::rowCount return 0:

            You have to wait for QFileSystemModel::directoyLoaded()

            OK, however in the doc Model/View Programming:

            QFileSystemModel *model = new QFileSystemModel;
            QModelIndex parentIndex = model->index(QDir::currentPath());
            int numRows = model->rowCount(parentIndex);
            

            PS: to locate the code, search numRows, the first one.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @jronald , @Christian-Ehrlicher
            You are right, that example (at link https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes) is not accurate for QFileSystemModel behaviour, it's intended to illustrate the principle of the code it's showing but has picked an unsuitable model.

            From https://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance:

            Unlike QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.

            Perhaps therefore the docs example could just substitute QDirModel for QFileSystemModel in that light?

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB No, the class is obsolete. The example should either use a different model or take into account the asynchronous nature of QFileSystemModel.

              I'd go with the later.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Documentation update is in progress.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

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