QFileSystemModel中通过flags函数反应代码的层级思考
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.