如何在 PyQt 中的自定义 Qt.ItemFlags 实例中找到特定的 Qt.ItemFlag 出现?

2024-03-31

我有一个QTreeWidget http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qtreewidget.html with QTreeWidgetItem http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qtreewidgetitem.html。方法QTreeWidgetItem.flags() http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtreewidgetitem.html#flags返回我Qt.ItemFlags http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt-itemflags.html实例。

The Qt.ItemFlags http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt-itemflags.htmltype 是 QFlags 的类型定义。它存储一个 OR 组合ItemFlag http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt.html#ItemFlag-enum values.

好吧,假设现在我有我的一个QTreeWidgetItem http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qtreewidgetitem.html我需要弄清楚——它是否可以检查?换句话说,我需要找到出现的情况ItemFlag http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt.html#ItemFlag-enum(就我而言,它是Qt.ItemIsUserCheckable http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt.html#ItemFlag-enum) in Qt.ItemFlags http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt-itemflags.html实例。

怎么做?

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

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)

按位掩码 http://en.wikipedia.org/wiki/Mask_%28computing%29

它的工作原理如下:

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.

所以该值将是这些值的某种组合。可以说它是可选择的、可编辑的并且已启用。为简单起见,我将仅使用数值而不是标志。我们用逻辑或将它们组合起来:

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 运算来切换标记中的标志状态。

# toggle off the editable flag
flags ^= 2
# toggle on the editable flag
flags ^= 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 PyQt 中的自定义 Qt.ItemFlags 实例中找到特定的 Qt.ItemFlag 出现? 的相关文章

  • 仅在发布模式下使用 glGenBuffer 时出现未处理的异常 - QT

    我在 Windows 7 上使用 Qt 4 8 发布模式编译项目时遇到了一些问题 调试时一切正常 但在发布时我收到未处理的异常 0xC0000005 访问冲突 我将范围缩小到发生这种情况的行 即生成像素缓冲区的时间 我的第一个猜测是 DLL
  • 仅在内部/外部抚摸路径?

    Given a QPainterPath http qt project org doc qt 4 8 qpainterpath html如何仅在路径的内侧或外侧边缘 或非闭合路径的左侧或右侧 描边路径 QPainter strokePat
  • 用 C++/Qt 编写的程序中的 RTF / doc / docx 文本提取

    我正在写一些程序Qt https en wikipedia org wiki Qt 28software 29 C 我需要从中读取文本微软Word https en wikipedia org wiki Microsoft Word RTF
  • new 运算符(以及 malloc)无法分配约 450 MB 的内存 [重复]

    这个问题在这里已经有答案了 我正在开发一个程序 该程序在内存中存储大约 2 2 亿个短值的数组 该数据块的分配方式如下 short arrayName new short SIZE OF ARRAY 然后将文件的内容读入内存 在团队中的另一
  • 第一次信号发射后自动断开

    我正在从文件加载网页 然后替换其中的一些 html self template web page QtWebKit QWebPage self template web page mainFrame load QtCore QUrl tem
  • Retina 显示屏中具有 QOpenGLWIdget 的 Qt MainWindow 显示错误大小

    我有一个 Qt 应用程序MainWindow 我嵌入一个QOpenGLWidget在里面 一切正常 直到我开始使用 Apple Retina 显示屏并在高 DPI 模式下运行我的应用程序 我的QOpenGLWidget只是它应该具有的大小的
  • QMainWindow 上的 Qt 布局

    我设计了一个QMainWindow with QtCreator s设计师 它由默认的中央小部件 aQWidget 其中包含一个QVBoxLayout以及其中的所有其他小部件 现在我想要的一切就是QVBoxLayout自动占据整个中央小部件
  • 如何在针对 Windows XP 的情况下使用 VS2012 构建 Qt 4/5?

    我正在尝试使用 Visual Studio 2012 构建 Qt 4 8 5 Qt 5 2 1 针对 Windows XP SDK v7 1a 使用 VS2102 编译时 源代码与 SDK v7 1a 存在各种不兼容性 因此无法开箱即用 这
  • 将 jstring 转换为 QString

    我正在调用一个返回字符串的 Java 函数 QAndroidJniObject obj QAndroidJniObject callStaticObjectMethod
  • PyQt4 QPalette 不工作

    btn QtGui QPushButton Button self palettes btn palette palettes setColor btn backgroundRole QtCore Qt green btn setPalet
  • 如何在 Qt 应用程序中嵌入 Python 解释器?

    有没有一种简单的方法可以将 Python 解释器嵌入到 Qt 应用程序中 如果可能的话 我希望有一个跨平台的解决方案 这就是目的PythonQt http pythonqt sourceforge net 它支持 Windows Linux
  • 是否有 Qt 小部件可以浏览应用程序中小部件的层次结构(类似于 Spy++)?

    我们有一个具有复杂的小部件层次结构的应用程序 我希望能够以与 Spy 类似的方式浏览此层次结构 查看和编辑属性 例如大小 如果有一个小部件可以显示此信息 则它不需要在外部应用程序中运行 那么问题来了 这样的神兽存在吗 您可以使用Gammar
  • Qt中Q_PROPERTY的意义是什么?

    我无法理解 Q PROPERTY 的用法 Q PROPERTY 如何帮助程序具有防御性 它是干什么用的 我看过这个论坛 但确实无法应用 我已经理解了这个例子 但不明白它的用法 这是一个例子 我能从中得到什么 我知道阅读将赋予只读特权 wri
  • 连接到 QNetworkReply::error 信号

    我正在使用 Qt5 的新连接语法 QNetworkReply 有一个名为error http qt project org doc qt 5 0 qtnetwork qnetworkreply html error 2还有一个函数叫做err
  • 使用 Matplotlib、PyQt 和 Threading 进行实时绘图导致 python 崩溃

    我一直在努力研究我的 Python 应用程序 但找不到任何答案 我有 PyQT GUI 应用程序 它使用 Matplotlib 小部件 GUI 启动一个新线程来处理 mpl 小部件的绘图 恐怕我现在通过从另一个线程访问 matplotlib
  • Qt QML 数据模型似乎不适用于 C++

    我一直在使用中的示例http doc qt digia com 4 7 qdeclarativemodels html http doc qt digia com 4 7 qdeclarativemodels html这是 QML 声明性数
  • QFileSystemModel setRootPath

    我正在尝试创建一个 Qt 应用程序来显示文件夹 Mac OS 中的 Users 文件夹 的内容 这是代码 QFileSystemModel dirModel new QFileSystemModel dirModel gt setRootP
  • 通过单击内部小部件而不是标题栏来移动窗口

    在 Windows 中 当我创建 QMainWindow 时 我可以通过单击标题栏并拖动它来在屏幕上移动它 在我的应用程序中 我使用隐藏了标题栏setWindowFlags Qt CustomizeWindowHint 我正在尝试使用小部件
  • Q风格所有权

    在 Qt 应用程序中使用样式时 我遇到了一个有趣的问题QStyle所有权 QStyle继承自QObject 通常接受QObject parent作为构造函数参数来管理其子级的生命周期 但QStyle的构造函数没有此构造函数参数 第一个问题
  • 在 Qt 中自动调整标签文本大小 - 奇怪的行为

    在 Qt 中 我有一个复合小部件 它由排列在 QBoxLayouts 内的多个 QLabels 组成 当小部件调整大小时 我希望标签文本缩放以填充标签区域 并且我已经在 resizeEvent 中实现了文本大小的调整 这可行 但似乎发生了某

随机推荐