Solved QTreeView rowCount() value differs
-
@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 ???
-
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-shotQTimer
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...
-
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 theexpanded
slot and that should be called after the expand has been acted on, I assume. -
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 firstdirectoryLoaded
corresponds to separate thread actually reading from the OS file system. That takes "a bit of time". But subsequentdirectoryLoaded
signals just correspond toQFileSystemModel
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. -