Pyqt5 QTreeWIdgetItem 一次选择一项复选框

问题描述 投票:0回答:2

我有一个 QTreeWidget,每个 QTreeWidgetItem 有 3 个复选框。我想做两件事,但我不知道如何做。

  1. 用户一次只能选择一个复选框 每个 QTreeWidgetItem.So 选择一个给定的复选框 QTreeWidgetItem 应该取消选择该控件的其他复选框 QTreeWidgetItem.
  2. 选择/取消选择给定的父 QTreeWidgetItem 复选框 应选择/取消选择同一列中的所有子复选框。 下面是我的代码:

任何帮助将不胜感激

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

def main():
    app     = QtWidgets.QApplication(sys.argv)
    tree    = QtWidgets.QTreeWidget()
    tree.resize(500, 200)
    headerItem  = QtWidgets.QTreeWidgetItem()
    item    = QtWidgets.QTreeWidgetItem()
    tree .setColumnCount(4)
    tree .setHeaderLabels(["pluto", "X", "Y", "Z", ""])
    for i in range(3):
        parent = QtWidgets.QTreeWidgetItem(tree)
        parent.setText(0, "Parent {}".format(i))
        parent.setCheckState(1, Qt.Unchecked)
        parent.setCheckState(2, Qt.Unchecked)
        parent.setCheckState(3, Qt.Unchecked)
        #parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
        for x in range(5):
            child = QtWidgets.QTreeWidgetItem(parent)
            child.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
            child.setText(0, "Child {}".format(x))
            child.setCheckState(1, Qt.Unchecked)
            child.setCheckState(2, Qt.Unchecked)
            child.setCheckState(3, Qt.Unchecked)


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

if __name__ == '__main__':
    main()
python pyqt5 qtreewidget qtreewidgetitem
2个回答
0
投票

我已经通过使用 itemClicked 解决了我自己的问题。这是我的解决方案,以防有人感兴趣。

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

class treeManagement:
    def __init__(self):
        app     = QtWidgets.QApplication(sys.argv)
        self.tree    = QtWidgets.QTreeWidget()
        self.tree.resize(500, 200)
        headerItem  = QtWidgets.QTreeWidgetItem()
        item    = QtWidgets.QTreeWidgetItem()
        self.tree .setColumnCount(4)
        self.tree .setHeaderLabels(["pluto", "X", "Y", "Z", ""])
        for i in range(3):
            parent = QtWidgets.QTreeWidgetItem(self.tree)
            parent.setText(0, "Parent {}".format(i))
            parent.setCheckState(1, Qt.Unchecked)
            parent.setCheckState(2, Qt.Unchecked)
            parent.setCheckState(3, Qt.Unchecked)

            for x in range(5):
                child = QtWidgets.QTreeWidgetItem(parent)
                child.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
                child.setText(0, "Child {}".format(x))
                child.setCheckState(1, Qt.Unchecked)
                child.setCheckState(2, Qt.Unchecked)
                child.setCheckState(3, Qt.Unchecked)

        self.tree.itemClicked.connect(self.checkboxSelectionChanged)
        self.tree.show()
        sys.exit(app.exec_())

    def checkboxSelectionChanged(self,selectedItem, selectedColumn):
        print(selectedItem.text(0))
        print(selectedColumn)
        # In this method I manage checkbox selected

        if selectedItem is None:
            return

        # 0-The item is unchecked.
        # 1-The item is partially checked. Items in hierarchical models may be partially checked if some, but not all,
        # of their children are checked.
        # 2-The item is checked.

        sumCheckState = selectedItem.checkState(1) + selectedItem.checkState(2) + selectedItem.checkState(3)

        state = QtCore.Qt.Unchecked
        for i in range(1, 4):
            if selectedColumn != i:
                if sumCheckState >= 4:  # Prevent update if user clicked in the space near checkbox or there's only one
                    # checkbox currently checked in this row
                    selectedItem.setCheckState(i, QtCore.Qt.Unchecked)
            elif selectedItem.childCount() > 0:  # selectedColumn=i
                for j in range(selectedItem.childCount()):
                    child = selectedItem.child(j)
                    for k in range(1, 4):
                        if selectedColumn != k:
                            if sumCheckState >= 4 or (sumCheckState == 2 and child.checkState(k) == QtCore.Qt.Checked
                                and selectedItem.checkState(selectedColumn) != QtCore.Qt.Unchecked):
                                # Prevent update if user clicked in the space near checkbox or there's only one
                                # checkbox currently checked in this row
                                child.setCheckState(k, QtCore.Qt.Unchecked)
                        else:
                            child.setCheckState(k, selectedItem.checkState(selectedColumn))
            elif selectedItem.parent() is not None:
                # I have clicked on a child element so no parent should be in checked state
                if sumCheckState >= 4:  # Prevent update if user clicked in the space near checkbox or there's only one
                    # checkbox currently checked in this row
                    for i in range(1, 4):
                        selectedItem.parent().setCheckState(i, QtCore.Qt.Unchecked)

if __name__ == '__main__':
    t=treeManagement()

0
投票

创建一个按钮组并将其独占属性设置为true,然后将所有复选框添加到其中。

self.buttonGroup = QButtonGroup(self)
self.buttonGroup.setExclusive(True)
self.buttonGroup.addButton(checkBox1)
self.buttonGroup.addButton(checkBox2)
...
© www.soinside.com 2019 - 2024. All rights reserved.