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 sh开发者_如何转开发ow only local drives?
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.
How about using QFileSystemModel filter to only QDir::Drives. To my knowledge, this lists only the local hard drives.
精彩评论