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 not updated until 2nd click?
QtWS25 Last Chance

QFileSystemModel rowCount not updated until 2nd click?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.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.
  • Q Offline
    Q Offline
    qDebug
    wrote on last edited by qDebug
    #1

    I'm new to Qt and seek for help. Everything so far works just fine but...

    void MainWindow::on_treeView1_clicked(const QModelIndex &index)
    {
        QString sPath = dirmodel->fileInfo(index).absoluteFilePath();
        ui->listView1->setRootIndex(filemodel->setRootPath(sPath));
    
        ui->treeView1->expand(dirmodel->setRootPath(sPath));
    
        QModelIndex parentIndex = ui->listView1->rootIndex();
        int numRows = filemodel->rowCount(parentIndex);
    
        for (int row = 0; row < numRows; ++row)
        {
            QModelIndex index2 = filemodel->index(row, 0, parentIndex);
            QString teXt = filemodel->data(index2, Qt::DisplayRole).toString();
            qDebug() << teXt;
        }
    
        qDebug() << numRows;
    }
    

    ... numRows does not update until the second click on a path. On the first click listView1 gets updated with the files in the current path but numRows are still 0. If i click a 2nd time on the link or come back from another path, numRow is correct.

    Any ideas?

    Thanks!

    1 Reply Last reply
    0
    • ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by ValentinMichelet
      #2

      Hi, welcome to Devnet.

      I'm not sure but QFileSystemModel is using lazy loading to display only unfolded directories. The reason is simple: imagine if a user sets the model root to their computer's root, it would consume a lot of resources to display their whole computer hierarchy. Therefore, QFileSystemModel is threaded and run asynchronously. This enables to prevent your interface from freezing if there are a lot to display when the user unfolds a directory.

      With that in mind, I would say that numRows is not updated when you think it would be. But it's only a guess, I can't explain why it's done correctly the second time. I'm not even sure that this behavior is the same on every computer, with any root directory.

      Q 1 Reply Last reply
      0
      • ValentinMicheletV ValentinMichelet

        Hi, welcome to Devnet.

        I'm not sure but QFileSystemModel is using lazy loading to display only unfolded directories. The reason is simple: imagine if a user sets the model root to their computer's root, it would consume a lot of resources to display their whole computer hierarchy. Therefore, QFileSystemModel is threaded and run asynchronously. This enables to prevent your interface from freezing if there are a lot to display when the user unfolds a directory.

        With that in mind, I would say that numRows is not updated when you think it would be. But it's only a guess, I can't explain why it's done correctly the second time. I'm not even sure that this behavior is the same on every computer, with any root directory.

        Q Offline
        Q Offline
        qDebug
        wrote on last edited by qDebug
        #3

        @ValentinMichelet

        I was curious and did set a wait 1000ms after

        ui->treeView1->expand(dirmodel->setRootPath(sPath));
        

        and the result was a freezing app. Nothing was updated until the second was over. Well, OK, if all the files are loaded in listView1 after treeView1 was clicked the reading from the file system should be done. So i did try to find a way / slot like "on_listView1_is_updated" but no luck so far.

        Or is there a better way to loop through files after QFileSystemModel is done?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qDebug
          wrote on last edited by
          #4

          Anyone got a hint how to access data processed by the QFileSystemModel? So far i'm only successful filling lists but processing data fails. Is there any way to hook into a listView and get the data out of there after it was updated by QFileSystemModel?

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rturrentine
            wrote on last edited by
            #5

            Not sure if you solved this or not but QFileSystemModel emits the directoryLoaded signal once the background thread has completed loading the directory. Would that work for you?

            ValentinMicheletV 1 Reply Last reply
            1
            • R rturrentine

              Not sure if you solved this or not but QFileSystemModel emits the directoryLoaded signal once the background thread has completed loading the directory. Would that work for you?

              ValentinMicheletV Offline
              ValentinMicheletV Offline
              ValentinMichelet
              wrote on last edited by
              #6

              @rturrentine
              Excellent!

              @qDebug
              Just connect this signal to a slot that updates the numRows and it should work just fine.

              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