I'm using a custom subclass QFileSystemModel in a treeview to allow users to select folders using checkboxes. My problem is that Mapped network drives are listed as hard drives, but I cannot operate on them, So I want to only show local drives. How can I force QFileSystemModel to show only local drives?
2 Answers
From what I've been able to figure out from sources and docs, it is impossible to do directly with QFileSystemModel.
The only possible workaround I can think of is to use a QSortFilterProxyModel subclass with redefined filterAcceptsRow() or filterAcceptsColumn() or both, I'm not sure which one as I haven't used QFileSystemModel and don't know what it considers columns and what rows. The redefined method would then figure out the path of the file possibly by using data() with QFileSystemModel::FilePathRole or by using qobject_cast<> and calling QFileSystemModel::filePath(). Then it would somehow figure out the root path of a drive and call GetDriveType() WinAPI function as Qt apparently provides no way to figure out whether a drive is network or not. Of course, this stuff has to be put under #ifdef Q_OS_WIN32
.
This looks complicated, but I can think of no other way. I'm also not sure how fast GetDriveType() is, but if there are performance problems it is always possible to implement some kind of caching.

- 24,345
- 8
- 57
- 73
-
1filterAcceptsRow() needed to be redefined. I've implemented it and it works perfectly. – ShayanOH Jan 14 '11 at 11:22
How about using QFileSystemModel filter to only QDir::Drives. To my knowledge, this lists only the local hard drives.

- 992
- 1
- 8
- 15
-
If you have a mapped network drives in windows, it will show in QDir::Drives, so this won't work. The best solution was the previous one, using GetDriveType in a QSortFilterProxyModel. I've implemented it and it works perfectly and without any performance penalty. – ShayanOH Jan 14 '11 at 11:21