2

对于这个问题,我指的是@Andy PyQt Tree Widget的答案,添加了动态删除的复选框

@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()

上面代码的运行结果:

在此处输入图像描述

更新:所需的结果应如下所示... 在此处输入图像描述

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

4

1 回答 1

5

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

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()

在此处输入图像描述

于 2018-02-02T05:32:28.017 回答