【问题标题】:PyQt [QTreeWidget]: How to add Radiobutton for items?PyQt [QTreeWidget]:如何为项目添加单选按钮?
【发布时间】:2018-02-02 03:30:05
【问题描述】:

这个问题我指的是@Andy PyQt Tree Widget, adding check boxes for dynamic removal的回答

@Andy 展示了如何将CheckBox 添加到QTreeWidget 中,效果很好。

我想在这里问一下,如何将RadioButton添加到QTreeWidget中? ----而且,这对我来说更难,如何只选择一个项目,尽管它们在不同的groups 中?

我将@Andy 的代码重写为PyQt5:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

def main(): 
    app     = QApplication (sys.argv)
    tree    = QTreeWidget ()
    headerItem  = QTreeWidgetItem()
    item    = QTreeWidgetItem()

    for i in range(3):
        parent = QTreeWidgetItem(tree)
        parent.setText(0, "Parent {}".format(i))
        parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
        for x in range(5):
            child = QTreeWidgetItem(parent)
            child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
            child.setText(0, "Child {}".format(x))
            child.setCheckState(0, Qt.Unchecked)
    tree.show() 
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

上面代码的运行结果:

更新:所需的结果应如下所示...

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你可以解释一下它的含义:如何只选择一个项目,尽管它们在不同的组中?
  • 什么是组,在另一个组中是什么意思?您的意思是每个组都与 parent0、parent1 等相关联,并且相同的项目是例如具有相同名称的那些?
  • 你想让父母也有QRadioButton吗?
  • 所有父母都会有相同的孩子?
  • @eyllanesc 感谢您的回答。我在上面添加了一张图片来解释我的问题。请看一看。组的名称并不重要。总之,我的问题是:1 如何添加 Radiobutton,以及 2 如何使 Radiobutton '唯一'。

标签: python pyqt pyqt5 qtreewidget qradiobutton


【解决方案1】:

如果你想建立一个QRadioButton并且它在一个组中是独占的,一个可能的解决方案是实现一个委托,在这个委托中我们将覆盖paint方法来绘制QRadioButton和@987654326 @方法捕捉点击事件,并根据情况改变其他项目的状态。

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class Delegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        if not index.parent().isValid():
            QStyledItemDelegate.paint(self, painter, option, index)
        else:
            widget = option.widget
            style = widget.style() if widget else QApplication.style()
            opt = QStyleOptionButton()
            opt.rect = option.rect
            opt.text = index.data()
            opt.state |= QStyle.State_On if index.data(Qt.CheckStateRole) else QStyle.State_Off
            style.drawControl(QStyle.CE_RadioButton, opt, painter, widget)

    def editorEvent(self, event, model, option, index):
        value = QStyledItemDelegate.editorEvent(self, event, model, option, index)
        if value:
            if event.type() == QEvent.MouseButtonRelease:
                if index.data(Qt.CheckStateRole) == Qt.Checked:
                    parent = index.parent()
                    for i in range(model.rowCount(parent)):
                        if i != index.row():
                            ix = parent.child(i, 0)
                            model.setData(ix, Qt.Unchecked, Qt.CheckStateRole)

        return value


def main():
    app = QApplication(sys.argv)
    tree = QTreeWidget()
    tree.setItemDelegate(Delegate())

    for i in range(3):
        parent = QTreeWidgetItem(tree)
        parent.setText(0, "Parent {}".format(i))
        for x in range(5):
            child = QTreeWidgetItem(parent)
            child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
            child.setText(0, "Child {}".format(x))
            child.setCheckState(0, Qt.Unchecked)

    tree.expandAll()
    tree.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

【讨论】:

  • 非常感谢!
  • It also helps to add a check on uncheck status, so when the same item is selected, the radio button doesn't disappear.
  • ` if index.data(QtCore.Qt.CheckStateRole) == QtCore.Qt.Unchecked: parent = index.parent() for i in range(model.rowCount(parent)): if i == index.row(): ix = parent.child(i, 0) model.setData(ix, QtCore.Qt.Checked, QtCore.Qt.CheckStateRole)`
猜你喜欢
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 2017-02-07
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多