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. QTreeView rowCount() value differs
Forum Updated to NodeBB v4.3 + New Features

QTreeView rowCount() value differs

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 4.9k 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by Vinoth Rajendran4
    #1

    Hi All,
    I have QTreeView ,with QFileSystemModel as its model.

    model = new QFileSystemModel(this);
    model->setRootPath("");
    ui->treeView->setRootIndex(model->index(""));
    

    I have connected QTreeView expanded signal with a slot in my Class,

    connect(ui->treeView,SIGNAL(expanded(const QModelIndex&)),this,SLOT(expand(const QModelIndex&)));
    

    When i try expand my MEDIA Drive which has 3 folders, again and again, my values differ.

    My Slot function is as follow,

    void MainWindow::expand(const QModelIndex &index)
    {
        qDebug() << model->data(index,Qt::DisplayRole);
        qDebug() << model->hasChildren(index);
        qDebug() << model->hasIndex(0,0,index);
        qDebug() << model->rowCount(index) ;
    }
    

    during first expansion, the values of rowCount() is 0

    QVariant(QString, "MEDIA (F:)")
    true
    false
    0

    for the subsequent expansion, value of rowCount() is 3

    QVariant(QString, "MEDIA (F:)")
    true
    true
    3

    I would like to know why rowCount() and hadIndex() value differs , is it a bug?? or am i missing something ...

    Qt 5.9.1 , Windows 10 is used.

    Any Help is deeply appreciated...

    Thank You!

    JonBJ 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      Hi All,
      I have QTreeView ,with QFileSystemModel as its model.

      model = new QFileSystemModel(this);
      model->setRootPath("");
      ui->treeView->setRootIndex(model->index(""));
      

      I have connected QTreeView expanded signal with a slot in my Class,

      connect(ui->treeView,SIGNAL(expanded(const QModelIndex&)),this,SLOT(expand(const QModelIndex&)));
      

      When i try expand my MEDIA Drive which has 3 folders, again and again, my values differ.

      My Slot function is as follow,

      void MainWindow::expand(const QModelIndex &index)
      {
          qDebug() << model->data(index,Qt::DisplayRole);
          qDebug() << model->hasChildren(index);
          qDebug() << model->hasIndex(0,0,index);
          qDebug() << model->rowCount(index) ;
      }
      

      during first expansion, the values of rowCount() is 0

      QVariant(QString, "MEDIA (F:)")
      true
      false
      0

      for the subsequent expansion, value of rowCount() is 3

      QVariant(QString, "MEDIA (F:)")
      true
      true
      3

      I would like to know why rowCount() and hadIndex() value differs , is it a bug?? or am i missing something ...

      Qt 5.9.1 , Windows 10 is used.

      Any Help is deeply appreciated...

      Thank You!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Vinoth-Rajendran4
      To be clear: when it returns 0 first time, how do we know from your code just which node is being expanded? Could you debug out the index or the label of the item or similar?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        I'm not 100% sure but I think it has to do with lazy loading (QFileSystemModel::fetchMore). When you expand the first time the model did not actually "read" those rows yet.

        If this is the case you can use

        void MainWindow::expand(const QModelIndex &index)
        {
        while(model->canFetchMore(index))
        model->fetchMore(index);
            qDebug() << model->data(index,Qt::DisplayRole);
            qDebug() << model->hasChildren(index);
            qDebug() << model->hasIndex(0,0,index);
            qDebug() << model->rowCount(index) ;
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        V 1 Reply Last reply
        0
        • VRoninV VRonin

          I'm not 100% sure but I think it has to do with lazy loading (QFileSystemModel::fetchMore). When you expand the first time the model did not actually "read" those rows yet.

          If this is the case you can use

          void MainWindow::expand(const QModelIndex &index)
          {
          while(model->canFetchMore(index))
          model->fetchMore(index);
              qDebug() << model->data(index,Qt::DisplayRole);
              qDebug() << model->hasChildren(index);
              qDebug() << model->hasIndex(0,0,index);
              qDebug() << model->rowCount(index) ;
          }
          
          V Offline
          V Offline
          Vinoth Rajendran4
          wrote on last edited by Vinoth Rajendran4
          #4

          @VRonin : Tried your approach, still issue is seen.

          model->canFetchMore(index)
          

          return false always....

          Is there any specific reason why model not reading its row contents during the very first expansion ???

          Thank You!

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Can you try adding Qt::QueuedConnection as the last argument to your connect and see if it works then?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            V 1 Reply Last reply
            0
            • VRoninV VRonin

              Can you try adding Qt::QueuedConnection as the last argument to your connect and see if it works then?

              V Offline
              V Offline
              Vinoth Rajendran4
              wrote on last edited by Vinoth Rajendran4
              #6

              @VRonin , with Qt::QueuedConnection also, issue is seen.

              Can this be a bug ???

              JonBJ VRoninV 2 Replies Last reply
              0
              • V Vinoth Rajendran4

                @VRonin , with Qt::QueuedConnection also, issue is seen.

                Can this be a bug ???

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Vinoth-Rajendran4

                Can this be a bug ???

                We don't know yet. Does it matter? At this point I assume you're looking for a workaround.

                Given that @VRonin's suggestions have not worked, at this point if it were me: I would test your code which outputs the numbers by calling it from your expand() via a single-shot QTimer with a delay of, say, a second. Let's see whether by any chance there's some OS activity going on which does not properly expand a top-level volume directory immediately and only really does it asynchronously? Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?

                V 1 Reply Last reply
                0
                • JonBJ JonB

                  @Vinoth-Rajendran4

                  Can this be a bug ???

                  We don't know yet. Does it matter? At this point I assume you're looking for a workaround.

                  Given that @VRonin's suggestions have not worked, at this point if it were me: I would test your code which outputs the numbers by calling it from your expand() via a single-shot QTimer with a delay of, say, a second. Let's see whether by any chance there's some OS activity going on which does not properly expand a top-level volume directory immediately and only really does it asynchronously? Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?

                  V Offline
                  V Offline
                  Vinoth Rajendran4
                  wrote on last edited by Vinoth Rajendran4
                  #8

                  @JonB : your workaround suggestion with single-shot timer , works fine.

                  Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?
                  

                  It happens on every directory node in the tree...

                  JonBJ 1 Reply Last reply
                  0
                  • V Vinoth Rajendran4

                    @JonB : your workaround suggestion with single-shot timer , works fine.

                    Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?
                    

                    It happens on every directory node in the tree...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Vinoth-Rajendran4

                    your workaround suggestion with single-shot timer , works fine.

                    So, let's be clear. You only get (normally) get a count of 0 first time. If you make it so the first time you examine the children is a second after the expansion was performed it does produce the correct count. Is that what you are saying?

                    If it happens on every directory, my impression is there would be code out there which simply does not work. It's like your expand() is being called at the very start of the expansion, but it's the expanded slot and that should be called after the expand has been acted on, I assume.

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

                      Hi,

                      Is Media a system disk or an external storage ?

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

                      JonBJ V 2 Replies Last reply
                      0
                      • V Vinoth Rajendran4

                        @VRonin , with Qt::QueuedConnection also, issue is seen.

                        Can this be a bug ???

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @Vinoth-Rajendran4 said in QTreeView rowCount() value differs:

                        Can this be a bug ???

                        Can you provide a minimum example to replicate the problem? if we can confirm the bug we cat take a look if it's solvable

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        V 2 Replies Last reply
                        0
                        • SGaistS SGaist

                          Hi,

                          Is Media a system disk or an external storage ?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @SGaist said in QTreeView rowCount() value differs:

                          Is Media a system disk or an external storage ?

                          I wondered about this (slow external media gets updated "slowly" under Windows). That's why I asked the OP when it happens

                          Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?

                          but he says

                          It happens on every directory node in the tree...

                          so assuming that's true it's not to do with media type.

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Hi,

                            Is Media a system disk or an external storage ?

                            V Offline
                            V Offline
                            Vinoth Rajendran4
                            wrote on last edited by
                            #13

                            @SGaist : Media is System disk

                            1 Reply Last reply
                            0
                            • VRoninV VRonin

                              @Vinoth-Rajendran4 said in QTreeView rowCount() value differs:

                              Can this be a bug ???

                              Can you provide a minimum example to replicate the problem? if we can confirm the bug we cat take a look if it's solvable

                              V Offline
                              V Offline
                              Vinoth Rajendran4
                              wrote on last edited by
                              #14

                              @VRonin : my entire code below,

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              
                                  model = new QFileSystemModel;
                                  model->setRootPath("");
                              
                                  ui->treeView->setModel(model);
                                  ui->treeView->setRootIndex(model->index(""));
                                  connect(ui->treeView,SIGNAL(expanded(QModelIndex)),this,SLOT(func(QModelIndex)));
                              }
                              
                              void MainWindow::func(QModelIndex index)
                              {
                                    qDebug() << model->data(index1,Qt::DisplayRole);
                                   qDebug() << model->hasChildren(index1);
                                   qDebug() << model->hasIndex(0,0,index1);
                                   qDebug() << model->rowCount(index1);
                              }
                              
                              1 Reply Last reply
                              0
                              • VRoninV VRonin

                                @Vinoth-Rajendran4 said in QTreeView rowCount() value differs:

                                Can this be a bug ???

                                Can you provide a minimum example to replicate the problem? if we can confirm the bug we cat take a look if it's solvable

                                V Offline
                                V Offline
                                Vinoth Rajendran4
                                wrote on last edited by
                                #15

                                @VRonin : the reason why rowcount() is 0 , might be justified with this link , http://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance .

                                Not sure though. Can you please provide your insight .

                                JonBJ 1 Reply Last reply
                                0
                                • V Vinoth Rajendran4

                                  @VRonin : the reason why rowcount() is 0 , might be justified with this link , http://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance .

                                  Not sure though. Can you please provide your insight .

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @Vinoth-Rajendran4
                                  That link includes:

                                  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.

                                  It says it's using a separate thread. You said you tried my suggestion of making the debug calls to the functions not start till a second after the expansion and that did make them return the correct answers. If you put that together with the separate thread....

                                  I think you should try putting a slot on the http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded signal. Let's see it report on which directories it finished loading when? Compare when those signals arrive to the value of your rowCount() etc.

                                  If that indeed explains the behaviour, you may have to rewrite your code a bit so that you use that signal in combination with the expanded one so that you can get your results reliably?

                                  V 1 Reply Last reply
                                  2
                                  • JonBJ JonB

                                    @Vinoth-Rajendran4
                                    That link includes:

                                    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.

                                    It says it's using a separate thread. You said you tried my suggestion of making the debug calls to the functions not start till a second after the expansion and that did make them return the correct answers. If you put that together with the separate thread....

                                    I think you should try putting a slot on the http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded signal. Let's see it report on which directories it finished loading when? Compare when those signals arrive to the value of your rowCount() etc.

                                    If that indeed explains the behaviour, you may have to rewrite your code a bit so that you use that signal in combination with the expanded one so that you can get your results reliably?

                                    V Offline
                                    V Offline
                                    Vinoth Rajendran4
                                    wrote on last edited by Vinoth Rajendran4
                                    #17

                                    @JonB : Seems, during the very first time expansion of a directory , slot of expanded() signal is called ahead of directoryLoaded() , hence the rowCount() is 0.
                                    But during subsequent expansion of the same directory, slot of directoryLoaded() is called ahead of expanded() signal , hence now i am receiving correct rowCount() value.

                                    Seems a little code alternation might do the trick.

                                    JonBJ 2 Replies Last reply
                                    1
                                    • V Vinoth Rajendran4

                                      @JonB : Seems, during the very first time expansion of a directory , slot of expanded() signal is called ahead of directoryLoaded() , hence the rowCount() is 0.
                                      But during subsequent expansion of the same directory, slot of directoryLoaded() is called ahead of expanded() signal , hence now i am receiving correct rowCount() value.

                                      Seems a little code alternation might do the trick.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @Vinoth-Rajendran4 That sounds exactly what I was anticipating....

                                      1 Reply Last reply
                                      1
                                      • V Vinoth Rajendran4

                                        @JonB : Seems, during the very first time expansion of a directory , slot of expanded() signal is called ahead of directoryLoaded() , hence the rowCount() is 0.
                                        But during subsequent expansion of the same directory, slot of directoryLoaded() is called ahead of expanded() signal , hence now i am receiving correct rowCount() value.

                                        Seems a little code alternation might do the trick.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #19

                                        @Vinoth-Rajendran4
                                        I imagine that the first directoryLoaded corresponds to separate thread actually reading from the OS file system. That takes "a bit of time". But subsequent directoryLoaded signals just correspond to QFileSystemModel looking up in its cache and seeing that directory has already been loaded. Hence the variation in the timing of when the signals arrive. Something like that.

                                        1 Reply Last reply
                                        1
                                        • V Offline
                                          V Offline
                                          Vinoth Rajendran4
                                          wrote on last edited by
                                          #20

                                          Thanks @JonB , @SGaist , @VRonin for your suggestions...

                                          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