内容简介:翻译自:https://stackoverflow.com/questions/13616329/how-to-find-specific-qt-itemflag-occurrence-into-custom-qt-itemflags-instance-in
我有一个 QTreeWidget
与 QTreeWidgetItems
.方法 QTreeWidgetItem.flags()
返回 Qt.ItemFlags
实例.
The 07003 type is a typedef for QFlags. It stores an OR combination of 07005 values.
那么,现在假设我有一个我的 QTreeWidgetItems ,我需要找出 – 它是否可以检查?换句话说,我需要在 Qt.ItemFlags 实例中找到事件 ItemFlag (对于我的情况,它是 Qt.ItemIsUserCheckable ).
怎么做?
这是一个简单而整洁的例子,你可以修改(感兴趣的部分用注释行标记):
import sys from PyQt4 import QtCore, QtGui class Window(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.treeWidget = QtGui.QTreeWidget() self.treeWidget.setHeaderHidden(True) self.addItems(self.treeWidget.invisibleRootItem()) self.treeWidget.itemClicked.connect (self.handleClicked) layout = QtGui.QVBoxLayout() layout.addWidget(self.treeWidget) self.setLayout(layout) def addItems(self, parent): column = 0 clients_item = QtGui.QTreeWidgetItem(parent, ['Clients']) clients_item.setData(column, QtCore.Qt.UserRole, 'data Clients') clients_item.setExpanded(True) item_1 = QtGui.QTreeWidgetItem(clients_item, ['Item 1']) item_1.setData(column, QtCore.Qt.UserRole, 'data Item 1') item_1.setCheckState(column, QtCore.Qt.Unchecked) item_2 = QtGui.QTreeWidgetItem(clients_item, ['Item 2']) item_2.setData(column, QtCore.Qt.UserRole, 'data Item 2') item_2.setCheckState(column, QtCore.Qt.Unchecked) def handleClicked(self, item, column): if item.checkState(column) == QtCore.Qt.Checked: print "checked", item, item.text(column) if item.checkState(column) == QtCore.Qt.Unchecked: print "NOT checked", item, item.text(column) # this part doesn't work =============================== # begin of part flags = item.flags() if QtCore.Qt.ItemIsUserCheckable in flags: print "is checkable", item, item.text(column) else: print "is NOT checkable", item, item.text(column) # end of part ========================================== if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
与Qt中的许多“标志”和“属性”一样,ItemFlags使用位掩码将多个选项组合成单个值.这可以使用按位运算符进行测试.
以下是检查项目是否可检查的方法:
flags = item.flags() if flags & QtCore.Qt.ItemIsUserCheckable: print "is checkable", item, item.text(column)
它的工作原理如下:
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.ItemIsTristate 64 The item is checkable with three separate states.
所以价值将是这些价值的某种组合.让我们说它是可选择的,可编辑的和启用的.为简单起见,我将只使用数值而不是标志.我们将它们与逻辑OR结合起来:
flags = 1 | 2 | 32 # 35
为了测试值的存在,我们使用逻辑AND:
# is it editable? flags & 2 # 2 # would evaluate to True # is it checkable? flags & 16 # 0 # would evaluate to False
您还可以使用XOR操作切换mak中的flags状态.
# toggle off the editable flag flags ^= 2 # toggle on the editable flag flags ^= 2
翻译自:https://stackoverflow.com/questions/13616329/how-to-find-specific-qt-itemflag-occurrence-into-custom-qt-itemflags-instance-in
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- SVG矢量图视窗viewBox,嵌套HTML综合实例:建立用户自定义相对坐标系统
- JVM指令分析实例三(方法调用、类实例)
- 通过实例入门Golang
- Iptables详解+实例
- elasticsearch之实例篇
- Kolla配置实例网络
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
图片转BASE64编码
在线图片转Base64编码工具
HTML 编码/解码
HTML 编码/解码