码迷,mamicode.com
首页 > 其他好文 > 详细

QFileSystemModel中通过flags函数反应代码的层级思考

时间:2018-08-24 10:53:45      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:mod   write   off   check   hand   sys   desc   select   depend   

 

  QFileSystemModel继承自QAbstractItemModel类,作为子类,需要实现一些成员函数。面向对象编程中,如何划分父类和子类的职责需要仔细权衡。拿flags函数的设计来说,目的是让model能获取肚子里的某一个node的信息。如果把它放在父类中,会出现什么问题呢?问题是,无法针对子类的个性而做出调整。

  在QFileSystemModel的flags函数里面,有这样一行:

    if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) {

  这里面用到了QFile::WriteUser,代表用户可以写这个node。正是由于需要根据model的项目而确定一些细节,所以要在子类中写这个flags函数。

另附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.

QFileSystemModel中通过flags函数反应代码的层级思考

标签:mod   write   off   check   hand   sys   desc   select   depend   

原文地址:https://www.cnblogs.com/microthink/p/9527566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!