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

QTreeView rowCount() value differs



  • 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!



  • @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?



  • 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) ;
    }
    


  • @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!



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



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

    Can this be a bug ???



  • @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)?



  • @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...



  • @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.


  • Lifetime Qt Champion

    Hi,

    Is Media a system disk or an external storage ?



  • @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



  • @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.



  • @SGaist : Media is System disk



  • @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);
    }
    


  • @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 .



  • @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?



  • @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.



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



  • @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.



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


Log in to reply