pySide2/pyqt5 学习记录

pyqt5 QTableWidgetItem setFlags () 设置实现

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 was 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. It stores an OR combination of ItemFlag values.

See also QAbstractItemModel.

所有的 flags 都是 Qt.ItemFlags () 对象,值为整数值。刚开始使用 setFlags () 这个命令时,单独使用 setFlags (Qt.ItemIsEditable) 会发现和内容描述的功能是相反的,如果要开启该功能,需要加上 Qt.ItemIsEnabled,即使用 setFlags (Qt.ItemIsEditable | Qt.ItemIsEnabled) 才能达到描述的功能。默认所有功能开启时 int (table.item (9, 0).flags ()) 值为 63,即包含了 Qt.ItemIsEnabled 在内的向上的所有功能,可转换为二进制编码理解。