我使用树窗口小部件构建树结构,默认情况下会检查所有项目。有一个列表可以保存一些应该取消选中的项目。我想要做的是在单击按钮时取消选中这些项目。我可以使用findItems(uncheckitems,Qt.MatchExactly | Qt.MatchRecursive,0)来查找对象。但是当我使用setCheckState(0,Qt.Unchecked)时,程序总是崩溃。
MENUS = [
("Action1", [
("Action4", [
("Action8", []), ("Action9", []), ("Action10", [])
]),
("Action6", [
("Action11", []), ("Action12", []), ("Action13", [])
]),
("Action7", [
("Action14", []), ("Action15", []), ("Action16", [])
]),
]),
("Action2", [
("Action17", [
("Action20", [
("Action25", []), ("Action26", []), ("Action27", [])
]),
("Action21", []),
("Action22", []),
]),
("Action18", [
("Action23", [
("Action28", []), ("Action29", []), ("Action30", [])
]),
("Action24", []),
]),
("Action19", []),
]),
("Action3", [
("Action31", [
("Action34", []), ("Action35", []), ("Action36", [])
]),
("Action32", [
("Action37", []), ("Action38", []), ("Action39", [])
]),
("Action33", []),
]),
]
uncheckedaction = ["Action3", "Action11", "Action29"]
from uitest import Ui_MainWindow
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QTreeWidgetItem,
)
from PyQt5.QtCore import Qt
class window(QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.loadMenus(MENUS)
self.ui.Menutree.itemChanged.connect(self.handleItemChanged)
self.ui.Uncheck.clicked.connect(self.uncheckprocess)
def uncheckprocess(self):
menutree = self.ui.Menutree
for uncheck in uncheckedaction:
uncheckeditem = menutree.findItems(uncheck, Qt.MatchExactly | Qt.MatchRecursive, 0)
print("uncheckeditem:", uncheckeditem)
uncheckeditem.setCheckState(0, Qt.Unchecked)
def loadMenus(self, menus):
def recurse(parent, items):
for text, children in items:
item = QTreeWidgetItem(parent)
item.setText(0, text)
item.setCheckState(0, Qt.Checked)
recurse(item, children)
recurse(self.ui.Menutree, menus)
self.ui.Menutree.expandAll()
def handleItemChanged(self, item, column):
def recurse(parent, state):
for index in range(parent.childCount()):
child = parent.child(index)
# child.setCheckState(0, state)
child.setDisabled(state == Qt.Unchecked)
recurse(child, state)
recurse(item, item.checkState(0))
if __name__ == "__main__":
app = QApplication([''])
gui = window()
gui.show()
app.exec_()
UI:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(816, 577)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 796, 468))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.Menutree = QtWidgets.QTreeWidget(self.scrollAreaWidgetContents)
self.Menutree.setObjectName("Menutree")
self.Menutree.headerItem().setText(0, "1")
self.Menutree.header().setVisible(False)
self.verticalLayout.addWidget(self.Menutree)
spacerItem = QtWidgets.QSpacerItem(20, 10, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.verticalLayout.addItem(spacerItem)
self.groupBox = QtWidgets.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.Previous = QtWidgets.QPushButton(self.groupBox)
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.Previous.setFont(font)
self.Previous.setObjectName("Previous")
self.horizontalLayout.addWidget(self.Previous)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem2)
self.Uncheck = QtWidgets.QPushButton(self.groupBox)
self.Uncheck.setObjectName("Uncheck")
self.horizontalLayout.addWidget(self.Uncheck)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem3)
self.Next = QtWidgets.QPushButton(self.groupBox)
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.Next.setFont(font)
self.Next.setObjectName("Next")
self.horizontalLayout.addWidget(self.Next)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem4)
self.Next_Save = QtWidgets.QPushButton(self.groupBox)
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.Next_Save.setFont(font)
self.Next_Save.setObjectName("Next_Save")
self.horizontalLayout.addWidget(self.Next_Save)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem5)
self.verticalLayout.addWidget(self.groupBox)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 816, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.TBlevel1 = QtWidgets.QToolBar(MainWindow)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.TBlevel1.sizePolicy().hasHeightForWidth())
self.TBlevel1.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(14)
self.TBlevel1.setFont(font)
self.TBlevel1.setObjectName("TBlevel1")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.TBlevel1)
self.TBlevel2 = QtWidgets.QToolBar(MainWindow)
font = QtGui.QFont()
font.setPointSize(12)
self.TBlevel2.setFont(font)
self.TBlevel2.setObjectName("TBlevel2")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.TBlevel2)
MainWindow.insertToolBarBreak(self.TBlevel2)
self.TBlevel3 = QtWidgets.QToolBar(MainWindow)
font = QtGui.QFont()
font.setPointSize(10)
self.TBlevel3.setFont(font)
self.TBlevel3.setObjectName("TBlevel3")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.TBlevel3)
MainWindow.insertToolBarBreak(self.TBlevel3)
self.TBlevel4 = QtWidgets.QToolBar(MainWindow)
self.TBlevel4.setObjectName("TBlevel4")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.TBlevel4)
MainWindow.insertToolBarBreak(self.TBlevel4)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Previous.setText(_translate("MainWindow", "Previous"))
self.Uncheck.setText(_translate("MainWindow", "Uncheck"))
self.Next.setText(_translate("MainWindow", "Next"))
self.Next_Save.setText(_translate("MainWindow", "Next and Save"))
self.TBlevel1.setWindowTitle(_translate("MainWindow", "toolBar"))
self.TBlevel2.setWindowTitle(_translate("MainWindow", "toolBar"))
self.TBlevel3.setWindowTitle(_translate("MainWindow", "toolBar_2"))
self.TBlevel4.setWindowTitle(_translate("MainWindow", "toolBar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
有人可以帮助我吗?非常感谢你。
答案 0 :(得分:1)
您正在查找对象列表,而不是单个对象:
def uncheckprocess(self):
menutree = self.ui.Menutree
for uncheck in uncheckedaction:
uncheckeditems = menutree.findItems(uncheck, Qt.MatchExactly | Qt.MatchRecursive, 0)
for uncheckeditem in uncheckeditems:
uncheckeditem.setCheckState(0, Qt.Unchecked)