PDA

View Full Version : QFileSystemModel: how can I be notified of directory listing updates?



truefusion
22nd July 2010, 20:12
Since QFileSystemModel does its work in a separate thread, the desired information from its indices cannot be fully obtained until it is done retrieving all the information; things like sorting or iterating through the directory listing isn't entirely possible until is has finished. I am wondering if it is possible to be notified of when the thread finishes so that i may at that point obtain a complete listing of the designated directory. Currently i can only retrieve the first file or directory within the current directory, but only after revisiting or refreshing the current directory can i get the complete listing.

squidge
22nd July 2010, 20:29
Sure it is, you just sort or interate through the contents as the model is updated. Why do you need to wait until it has finished updating?

truefusion
22nd July 2010, 21:23
Sure it is, you just sort or interate through the contents as the model is updated. Why do you need to wait until it has finished updating?
What method are you talking about exactly? I am using a QTreeView, QSortFilterProxyModel for sorting in the tree view, and a QFileSystemModel as the proxy model's source model. For sorting, when i set the root index on the tree view through QTreeView::setRootIndex(), and then call QTreeView::sortByColumn(), it does not entirely sort the way i want it to except only after re-setting the root index. Likewise, QTreeView::resizeColumnToContents() doesn't show any effect until re-setting.

And when it comes to obtaining the directory listing from the file system model through QFileSystemModel::index() (the one that takes/can take three parameters), it only ever returns the first file/directory, no other file/directory. However, as mentioned, i can obtain the complete directory listing using the very same methods when attempting to access the same directory a second time. For these reasons, i feel i require being informed of the thread finishing in order to obtain the desired result.

ChrisW67
23rd July 2010, 00:25
QSortFilterProxyModel::setDynamicSortFilter() useful?

truefusion
23rd July 2010, 09:23
QSortFilterProxyModel::setDynamicSortFilter() useful?
Unfortunately, that method is part of my code and bears the same effect.

wysota
23rd July 2010, 09:35
What "same effect" are you referring to?

truefusion
23rd July 2010, 16:09
What "same effect" are you referring to?
Concerning sorting, that would be the tree view listing doesn't sort the way i want it to (first by name, then by type). But sorting is of lower concern, as the user can just click the column headers to sort things their way. I can have everything working the way i want it to if i can delay calling the aforementioned methods till the file system model has finished generating all the information. I would rather avoid using QDir::entryList() as the file system model would already have the information i'm interested in.

wysota
23rd July 2010, 17:07
if i can delay calling the aforementioned methods
Which methods? setDynamicSortFilter()?

ChrisW67
24th July 2010, 05:44
I think the reference is to QTreeView::sortByColumn() but I am still having trouble seeing a problem that needs solving. For example, this works just fine for me;


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFileSystemModel f;
f.setRootPath("/");

QSortFilterProxyModel proxy;
proxy.setSourceModel(&f);
proxy.setDynamicSortFilter(true);

QTreeView t;
t.setModel(&proxy);
t.sortByColumn(0, Qt::DescendingOrder);
QModelIndex idx = proxy.mapFromSource(f.index("/usr/include"));
t.setRootIndex(idx);
t.show();

return app.exec();
}


Even on large folders the files are always sorted correctly.

truefusion
24th July 2010, 10:17
Which methods? setDynamicSortFilter()?
QTreeView::sortByColumn(), QFileSystemModel::index() and QTreeView::resizeColumnToContents().


I think the reference is to QTreeView::sortByColumn() but I am still having trouble seeing a problem that needs solving. For example, this works just fine for me;
[...]
Even on large folders the files are always sorted correctly.
After testing out your code, i figured out the problem with mine concerning sorting. Apparently, f->setRootPath(QDir::currentPath()) doesn't allow sorting the first time around (though this example is found in the documentation). Also, the test showed that setDynamicSortFilter() is irrelevant to the sorting and that resizeColumnToContents() does not resize the column properly. I'm still left with my other problem: i can't get a complete directory listing from the file system model the first time around.

wysota
24th July 2010, 14:24
i can't get a complete directory listing from the file system model the first time around.
Because you are calling the code too early. Qt models tell you when something changes in them and you either have to react on each of the changes of detect when the model stops signalling you that it is changing. Of course nothing guarantees that it will not change a second later but you can't solve it with resizeColumnToContents() anyway :) That's why we have QHeaderView::ResizeToContents resize mode :)

truefusion
25th July 2010, 13:01
Because you are calling the code too early. Qt models tell you when something changes in them and you either have to react on each of the changes of detect when the model stops signalling you that it is changing. Of course nothing guarantees that it will not change a second later but you can't solve it with resizeColumnToContents() anyway :) That's why we have QHeaderView::ResizeToContents resize mode :)
I know it is because i'm calling it too early. But i got it to work with the layoutChanged() signal of the model, though i would prefer for the signal to be emitted once.

And thanks for informing me about that enum.