在 QTreeView 上的子项上设置小部件

2023-12-01

谢谢这个线程,我可以将小部件添加到第二列或后续列QAbstractItemView(在我的例子中QTreeView) 的顶级项目view.

但是是否可以向子项添加小部件?

这是我尝试过的部分进展顺利的方法:

#!/usr/bin/env python
import os

from PyQt4.QtCore import QModelIndex, Qt
from PyQt4.QtGui import QApplication, QItemSelectionModel, \
                        QPushButton, QStandardItem, \
                        QStandardItemModel, QTreeView
from PyQt4.uic import loadUi


class PrvTreeviewNest(QTreeView):
    def __init__(self):
        super(PrvTreeviewNest, self).__init__()

        loadUi('/home/user/yourproject/resource/treeview_nest.ui')

        # row can be 0 even when it's more than 0.
        self._datamodel = QStandardItemModel(0, 2)
        self.setModel(self._datamodel)

        for i in range(4):
            self._add_widget(i + 1)

        self.show()

    def _add_widget(self, n):
        std_item = QStandardItem('{}th item'.format(n))
        self._datamodel.setItem(n, 0, std_item)

        node_widget = QPushButton('{}th button'.format(n))
        qindex_widget = self._datamodel.index(n, 1, QModelIndex())
        self.setIndexWidget(qindex_widget, node_widget)

        if n == 2:
            std_item_child = QStandardItem('child')
            std_item.appendRow(std_item_child)

            node_widget_child = QPushButton('petit button')
            qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
            self.setIndexWidget(qindex_widget_child, node_widget_child)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    # window = TreeviewWidgetSelectProve()
    window = PrvTreeviewNest()
    # window = TreeviewWidgetSelectProve()
    window.resize(320, 240)
    # window.show();
    window.setWindowTitle(
         QApplication.translate("toplevel", "Top-level widget"))
    # window.add_cols()

    sys.exit(app.exec_())

树视图_nest.ui可用。

您在下图中看到该项目child不显示按钮并且其父按钮被覆盖。显然我不知道如何为其编写代码。

enter image description here


Update)我想出了如何向孩子添加小部件。够棘手的,使用QStandardItem.insertRow与索引相结合即可完成工作。在我上面的示例代码中,替换_add_widget具有以下内容:

    def _add_widget(self, n):
        item_toplevel = QStandardItem('{}th item'.format(n))
        self._datamodel.setItem(n, 0, item_toplevel)

        widget_toplevel = QPushButton('{}th button'.format(n))
        qindex_toplevel = self._datamodel.index(n, 1, QModelIndex())
        self.setIndexWidget(qindex_toplevel, widget_toplevel)

        if n == 2:
            item_child_col0 = QStandardItem('child col0')
            item_child_col1 = QStandardItem('child col1')
            #item_toplevel.appendRow(item_child_col0)

            item_toplevel.insertRow(0, [item_child_col0, item_child_col1])

            widget_child = QPushButton('child widget')
            qindex_child = item_child_col1.index()
            self.setIndexWidget(qindex_child, widget_child)

我确信这不是最好/理想的设计方式,但似乎对我有用。我是在受到@Phlucious 的回答启发后想出的。谢谢!

enter image description here


你的错误就在这一行:

qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)

它为您提供第 2 行第 1 列的索引模型的,这是“第二项”,而不是您的孩子。您的孩子位于以下位置的第 1 行第 1 列std_item。在 Qt 中,尤其是在 QStandardItemModel 中,子项是相对于其父项存储的,而不是相对于模型存储的。

我对 PyQt 不太熟悉,无法为您提供确切的代码,但您应该能够执行以下操作:

qindex_widget_child = std_item_child.index(QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)

或者像这样:

qindex_widget_child = std_item.child(0, 1).index(QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 QTreeView 上的子项上设置小部件 的相关文章

  • 如何在 QTableView 标题中单击鼠标右键单击上下文菜单?

    下面的示例代码 很大程度上受到here http www saltycrane com blog 2007 12 pyqt 43 qtableview qabstracttablemodel 有一个右键单击上下文菜单 当用户单击表中的单元格
  • Pyqt-如何因另一个组合框数据而更改组合框数据?

    我有一个表 有 4 列 这 4 列中的两列是关于功能的 一个是特征 另一个是子特征 在每一列中 所有单元格都有组合框 我可以在这些单元格中打开txt 我想 当我选择电影院作为功能时 我只想看到子功能组合框中的电影名称 而不是我的 数据 中的
  • 如何检测QTableView中的双击

    我正在使用 PyQt 创建 GUI 应用程序 在继承自 QTableView 的视图中 需要检测用户双击行时选择的行 该表可以排序 但不能编辑 我该怎么做 注意 尝试了 doubleClicked int 信号 它是由鼠标按钮发出的 而不是
  • Pyqt5 中的 QThreads:这是官方 QThread 文档的正确 C++ 到 Python 翻译吗?

    关于如何实例化和使用的官方文档QThread可以在这里找到 http doc qt io qt 5 qthread html http doc qt io qt 5 qthread html 该文档描述了两种基本方法 1 工作对象方法和 2
  • 使用 PyQt 和 matplotlib 在可滚动小部件中显示多个绘图

    由于我没有得到答案this https stackoverflow com questions 12179893 creating a scrollable multiplot with pythons pylab我尝试用 PyQt 解决这
  • pyqt4窗口调整大小事件

    我正在使用 python3 和 pyqt4 我希望每次运行时都会运行一些代码QMainWindow已调整大小 我想要这样的东西 self window resized connect self resize but resized不是内置函
  • 在 pyqt4 应用程序中记录所有异常

    使用标准 python 日志记录 api 记录 pyqt4 应用程序中的所有异常的最佳方法是什么 我尝试将 exec 包装在 try except 块中 并记录其中的异常 但它只记录应用程序初始化时的异常 作为临时解决方案 我将最重要的方法
  • Qt Creator 中的 *.qss 文件没有语法突出显示?

    It s wired that Qt Creator didn t have a syntax highlight for its own style file format or did i missed some packages 版本
  • PyQt QFileDialog exec_ 很慢

    我正在使用自定义QFileDialog因为我想选择多个目录 但是exec 功能非常慢 我不明白为什么 我正在使用最新版本的 PyQt 代码片段 from PyQt4 import QtGui QtCore QtNetwork uic cla
  • 如何为 qDebug 重载运算符 <<

    我正在尝试为存储数据的类创建更有用的调试消息 我的代码看起来像这样 include
  • 带 Qt 的菜单栏/系统托盘应用程序

    我是 Qt PyQt 的新手 我正在尝试制作一个应用程序 其功能将从菜单栏 系统托盘执行 这里展示了一个完美的例子 我找不到关于如何做到这一点的好资源 有人可以建议吗 Thanks 我认为您正在寻找与QMenu and QMainWindo
  • 第一次信号发射后自动断开

    我正在从文件加载网页 然后替换其中的一些 html self template web page QtWebKit QWebPage self template web page mainFrame load QtCore QUrl tem
  • 如何在针对 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 存在各种不兼容性 因此无法开箱即用 这
  • 在 Qt 中使用多个不同的流读取同一文件

    使用 Qt 是否可以使用多个流读取文件以同时访问其中的不同数据部分 请注意 Qt 中的流 QTextStream QDataStream 不处理底层设备中的位置 流类只是一个包装器 用于更轻松地解析设备 QFile 实例 内的二进制数据 因
  • PyQt/PySide 中有默认图标吗?

    我正在阅读 PySide 上的教程 我在想 我是否需要为每件事找到自己的图标 或者是否有某种方法可以使用一些内置图标 这样 如果我希望我的小 GUI 在另一个桌面环境上运行 我就不需要找到一套全新的图标 您需要的是 Pyside QIcon
  • 如何从 C++ 程序中重新启动 Linux?

    我有一个 Qt 4 GUI 我需要在下拉菜单中提供一个选项 允许用户选择重新启动计算机 我意识到这对于以其他方式重新启动计算机的能力来说似乎是多余的 但选择需要保留在那里 我尝试使用 system 来调用以下内容 suid root she
  • 在 Qt 中自动调整标签文本大小 - 奇怪的行为

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

    我仍然很难理解 QAbstractItemModel 对项目的表示 有两种返回 QModelIndex 项的方法对我来说没有任何意义 QModelIndex QAbstractItemModel index int row int colu
  • 带上下文菜单的 QTreeWidget,无法获取正确的项目

    我有以下代码来创建QTreeWidget和一个包含 2 个操作的上下文菜单 import sys from PyQt5 import QtCore QtWidgets class Dialog QtWidgets QDialog def i
  • PyQt 使用 ctrl+Enter 触发按钮

    我正在尝试在我的应用程序中触发 确定 按钮 我当前尝试的代码是这样的 self okPushButton setShortcut ctrl Enter 然而 它不起作用 这是有道理的 我尝试查找一些按键序列here http ftp ics

随机推荐