Search code examples
qt4mapped-driveqfilesystemmodel

How to exclude mapped network drives from QFileSystemModel in QT?


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?


Solution

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