Solved QTreeView rowCount() value differs
-
@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. -