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.

QFileSystemModel中通过flags函数反应代码的层级思考的更多相关文章

  1. JS中深浅拷贝 函数封装代码

    一.了解 基本数据类型保存在栈内存中,按值访问,引用数据类型保存在堆内存中,按址访问. 二.浅拷贝 浅拷贝只是复制了指向某个对象的指针,而不是复制对象本身,新旧对象其实是同一内存地址的数据,修改其中一 ...

  2. [转]js中的字符串函数集和代码片段

    JS自带函数 concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = ...

  3. 在MongoDB中实现聚合函数 (转)

    随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加.这使得很多组织都在寻找一种经济的 ...

  4. 在MongoDB中实现聚合函数

    在MongoDB中实现聚合函数 随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加. ...

  5. python中实现延时回调普通函数示例代码

    python中实现延时回调普通函数示例代码 这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...

  6. Linux 编程中的API函数和系统调用的关系【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...

  7. 分类器是如何做检测的?——CascadeClassifier中的detectMultiScale函数解读

    原地址:http://blog.csdn.net/delltdk/article/details/9186875 在进入detectMultiScal函数之前,首先需要对CascadeClassifi ...

  8. FFmpeg: FFmepg中的sws_scale() 函数分析

    FFmpeg中的 sws_scale() 函数主要是用来做视频像素格式和分辨率的转换,其优势在于:可以在同一个函数里实现:1.图像色彩空间转换, 2:分辨率缩放,3:前后图像滤波处理.不足之处在于:效 ...

  9. LoadRunner中的Web 函数列表

    LoadRunner中的Web 函数列表 web test LoadRunner fuction_list D:\Program Files (x86)\Mercury Interactive\Mer ...

随机推荐

  1. Linux基础命令---lpc打印机控制

    lpc lpc指令用来控制打印机,它提供了一个命令行,用户可以输出命令来控制打印机.如果命令行上没有指定命令,lpc将从标准输入中显示提示符并接受命令. 此命令的适用范围:RedHat.RHEL.Ub ...

  2. Hdu2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包)

    Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品, ...

  3. [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器

    截至自2017-08-11,支持现世已出的几乎所有PC端浏览器版本判断. 受支持的PC端浏览器列表: Edge IE Chrome Firefox Opera Safari QQ浏览器 360系列浏览 ...

  4. 利用“Java同包同名类执行顺序”取消Java 网站应用程序Licence验证

    如果是在tomcat里运行,lib目录下一大堆的JAR包,不同的JAR包里可能会有相同的包名类名,JRE按照JAR名字的字母顺序加载JAR文件,同名类如果已加载,则后面的同名类会忽略. 公司购买的一款 ...

  5. Python爬虫(三)——开封市58同城出租房决策树构建

    决策树框架: # coding=utf-8 import matplotlib.pyplot as plt decisionNode = dict(boxstyle=') leafNode = dic ...

  6. [C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete

    程序清单4.1 #include<iostream> using namespace std; void main(){ ]; yams[]=; yams[]=; yams[]=; ]={ ...

  7. Linux 命令行下导入导出 .sql 文件

    一.导出数据库用的是 mysqldump 命令 1.导出数据和表结构 /usr/bin/mysqldump -u 用户名 -p 数据库名 > 数据库名.sql 敲回车键后会提示输入密码 注意 m ...

  8. Windows 用bat脚本带配置启动redis,并用vb脚本使其在后台运行。

    最近,在Windows上用开发PHP程序,需要用到Redis,每天要打开一个运行redis-server.exe的窗口这样比较烦,因为窗口就一直打开着,不能关闭. 所以就想着通过写脚本的方式,让他在后 ...

  9. java基础 (一)之HashMap

    HashMap的存储结构是由数组和链表共同完成.Entry<K,V>[] ,Entry是单向链表. 1 HashMap数据结构 HashMap的底层主要是基于数组和链表来实现的,它之所以有 ...

  10. 重写console.log的一些理解

    关于重写console.log的方式通常都是这样的: console.log = (function(oriLogFunc){ return function(str) { oriLogFunc.ca ...