如何在 PyQt 中的自定义 Qt.ItemFlags 实例中找到特定的 Qt.ItemFlag 出现? [英] How to find specific Qt.ItemFlag occurrence into custom Qt.ItemFlags instance in PyQt?

查看:26
本文介绍了如何在 PyQt 中的自定义 Qt.ItemFlags 实例中找到特定的 Qt.ItemFlag 出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTreeWidgetQTreeWidgetItems.方法 QTreeWidgetItem.flags() 返回我 Qt.ItemFlags 实例.

I have a QTreeWidget with QTreeWidgetItems. Method QTreeWidgetItem.flags() returns me Qt.ItemFlags instance.

Qt.ItemFlags 类型是 QFlags 的类型定义.它存储 ItemFlag 值的 OR 组合.

The Qt.ItemFlags type is a typedef for QFlags. It stores an OR combination of ItemFlag values.

好吧,假设现在我有一个 QTreeWidgetItems 并且我需要找出 - 是否可以检查?换句话说,我需要找到出现 ItemFlag(对于我的在这种情况下,它是 Qt.ItemIsUserCheckable) 在 Qt.ItemFlags 实例.

Well, assume now that I have one of my QTreeWidgetItems and I need to find out — is it checkable or not? Other words, I need to find occurrence ItemFlag (for my case, it's Qt.ItemIsUserCheckable) in Qt.ItemFlags instance.

怎么做?

这是一个简单而整洁的示例,您可以修改(注释行标记的感兴趣的部分):

Here's a simple and tidy example that you can modify (the interested part marked by comment lines):

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 使用位掩码将多个选项组合成一个值.这可以使用按位运算符进行测试.

Like many of the "flags" and "attributes" in Qt, ItemFlags uses bit masks to combine multiple options into a single value. This can be tested using bitwise operators.

以下是检查项目是否可检查的方法:

Here is how you would check if the item is checkable:

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 结合起来:

So the value will be some combination of these values. Lets say it is Selectable, Editable, and enabled. For simplicity I will just use the numeric values instead of the flags. We combine them with a logical OR:

flags = 1 | 2 | 32
# 35

为了测试值的存在,我们使用逻辑 AND:

And to test the presence of a value we use a logical AND:

# is it editable?
flags & 2
# 2 # would evaluate to True

# is it checkable?
flags & 16
# 0 # would evaluate to False

您还可以使用 XOR 操作切换 maks 中的标志状态.

You could also toggle the flags state in the maks using an XOR operation.

# toggle off the editable flag
flags ^= 2
# toggle on the editable flag
flags ^= 2

这篇关于如何在 PyQt 中的自定义 Qt.ItemFlags 实例中找到特定的 Qt.ItemFlag 出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆