QFileSystemModel繼承自QAbstractItemModel類,作為子類,需要實現一些成員函數。面向對象編程中,如何劃分父類和子類的職責需要仔細權衡。拿flags函數的設計來說,目的是讓model能獲取肚子里的某一個node的信息。如果把它放在父類中,會出現什麼問題呢?問題是,無法針對 ...
Qt的Model/View設計中,有一些隱藏的代碼,它們大多放在私有類里,對於類的作用非常關鍵,體現著Qt的整體設計思想。然而,由於它們比較隱蔽,學習起來比較繁瑣,受到人們的忽視。然而,體現設計思想,提高編程水平往往需要研讀深層次代碼。所謂奇偉鬼怪之觀,大多在於險遠,非有志者不能至也。
QFileSystemModel繼承自QAbstractItemModel類,作為子類,需要實現一些成員函數。面向對象編程中,如何劃分父類和子類的職責需要仔細權衡。拿flags函數的設計來說,目的是讓model能獲取肚子里的某一個node的信息。如果把它放在父類中,會出現什麼問題呢?問題是,無法針對子類的個性而做出調整。
在QFileSystemModel的flags函數裡面,有這樣一行:
if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) {
這裡面用到了QFile::WriteUser,代表用戶可以寫這個node。正是由於需要根據model的項目而確定一些細節,所以要在子類中寫這個flags函數。
在函數 QFileSystemModelPrivate::_q_fileSystemChanged中,調用了addNode函數。
void QFileSystemModelPrivate::_q_fileSystemChanged(const QString &path, const QVector<QPair<QString, QFileInfo> > &updates)
而在addNode函數中,又使用了populate函數,這個單詞的意思是“填充”,這個詞的意思對於理解這個函數的作用非常關鍵:對於一個文件系統的節點,它要麼是文件,要麼是文件夾,都需要一些信息來描述,比如對於文件夾,程式在運行的時候就會為其分配特定的圖標,而對於特定文件類型,則分配對於它的文件類型的圖標。除此之外,如果是文件,他就會有大小信息,這個信息也要在view中顯示出來。populate函數就是用來把文件的信息“填充”到節點裡面的。
另附Qt命名空間中,與flags相關的定義:
enum Qt::ItemFlag
flags Qt::ItemFlags
This enum describes the properties of an item:
Constant |
Value |
Description |
Qt::NoItemFlags |
0 |
It does not have any properties set. |
Qt::ItemIsSelectable |
1 |
It can be selected. |
Qt::ItemIsEditable |
2 |
It can be edited. |
Qt::ItemIsDragEnabled |
4 |
It can be dragged. |
Qt::ItemIsDropEnabled |
8 |
It can be used as a drop target. |
Qt::ItemIsUserCheckable |
16 |
It can be checked or unchecked by the user. |
Qt::ItemIsEnabled |
32 |
The user can interact with the item. |
Qt::ItemIsAutoTristate |
64 |
The item's state depends on the state of its children. This enables automatic management of the state of parent items in QTreeWidget (checked if all children are checked, unchecked if all children are unchecked, or partially checked if only some children are checked). |
Qt::ItemIsTristate |
ItemIsAutoTristate |
This enum value is deprecated. Use Qt::ItemIsAutoTristate instead. |
Qt::ItemNeverHasChildren |
128 |
The item never has child items. This is used for optimization purposes only. |
Qt::ItemIsUserTristate |
256 |
The user can cycle through three separate states. This value has been added in Qt 5.5. |
Note that checkable items need to be given both a suitable set of flags and an initial state, indicating whether the item is checked or not. This is handled automatically for model/view components, but needs to be explicitly set for instances of QListWidgetItem, QTableWidgetItem, and QTreeWidgetItem.
Note that it is undefined behavior to reimplement QAbstractItemModel::hasChildren to return true for an index if that index has the Qt::ItemNeverHasChildren flag set.
The ItemFlags type is a typedef for QFlags<ItemFlag>. It stores an OR combination of ItemFlag values.
See also QAbstractItemModel.
還有QFileDevice內部涉及到的定義:
enum QFileDevice::Permission
flags QFileDevice::Permissions
This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.
Constant |
Value |
Description |
QFileDevice::ReadOwner |
0x4000 |
The file is readable by the owner of the file. |
QFileDevice::WriteOwner |
0x2000 |
The file is writable by the owner of the file. |
QFileDevice::ExeOwner |
0x1000 |
The file is executable by the owner of the file. |
QFileDevice::ReadUser |
0x0400 |
The file is readable by the user. |
QFileDevice::WriteUser |
0x0200 |
The file is writable by the user. |
QFileDevice::ExeUser |
0x0100 |
The file is executable by the user. |
QFileDevice::ReadGroup |
0x0040 |
The file is readable by the group. |
QFileDevice::WriteGroup |
0x0020 |
The file is writable by the group. |
QFileDevice::ExeGroup |
0x0010 |
The file is executable by the group. |
QFileDevice::ReadOther |
0x0004 |
The file is readable by anyone. |
QFileDevice::WriteOther |
0x0002 |
The file is writable by anyone. |
QFileDevice::ExeOther |
0x0001 |
The file is executable by anyone. |
Warning: Because of differences in the platforms supported by Qt, the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version.
Note: On NTFS file systems, ownership and permissions checking is disabled by default for performance reasons. To enable it, include the following line:
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
Permission checking is then turned on and off by incrementing and decrementing qt_ntfs_permission_lookup by 1.
qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again
The Permissions type is a typedef for QFlags<Permission>. It stores an OR combination of Permission values.