我创建了一个带有项目(父项、子项)的 QTreeWidget,并且设置了一个复选框(如代码所示)。当检查一个项目时,我想使用 SQL 数据库执行命令。 (我知道 SQL 位),但我想知道如何找出某个项目是否已选中或未选中?我需要创建使用信号吗?
代码如下:
#Form ui
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.FilterList = QTreeWidget()
self.setCentralWidget(self.FilterList)
status = self.statusBar()
status.setSizeGripEnabled(False)
status.showMessage("Ready", 5000)
self.setWindowTitle("Form")
self.generateData()
def createMenusAndToolbars(self):
fileMenu = self.menuBar().addMenu("&File")
fileToolbar = self.addToolBar("File")
fileToolbar.setObjectName("FileToolbar")
for icon, text in (("new", "&New..."), ("open", "&Open..."),
("save", "&Save"), ("save", "Save &As..."),
(None, None), ("quit", "&Quit")):
if icon is None:
fileMenu.addSeparator()
else:
action = QAction(QIcon(":/file{}.png".format(icon)),
text, self)
if icon == "quit":
self.connect(action, SIGNAL("triggered()"),
self.close)
elif text != "Save &As...":
fileToolbar.addAction(action)
fileMenu.addAction(action)
editMenu = self.menuBar().addMenu("&Edit")
editToolbar = self.addToolBar("Edit")
editToolbar.setObjectName("EditToolbar")
for icon, text in (("add", "&Add..."), ("edit", "&Edit..."),
("delete", "&Remove")):
action = QAction(QIcon(":/edit{}.png".format(icon)),
text, self)
editToolbar.addAction(action)
editMenu.addAction(action)
def closeEvent(self, event):
if self.okToContinue():
settings = QSettings()
settings.setValue("MainWindow/Geometry", self.saveGeometry())
settings.setValue("MainWindow/State", self.saveState())
settings.setValue("MessageSplitter", self.QuestionSplitter.saveState())
settings.setValue("MainSplitter", self.mainSplitter.saveState())
else:
event.ignore()
def okToContinue(self):
return True
def generateData(self):
self.FilterList.setColumnCount(1)
self.FilterList.setHeaderLabels(["Filter Questions"])
ViewNode = QTreeWidgetItem(["View All"])
ViewNode.setCheckState(0, Qt.Checked)
DifficultyNode = QTreeWidgetItem(["Difficulty"])
TopicNode = QTreeWidgetItem(["Topic"])
TypeNode = QTreeWidgetItem(["Type"])
self.FilterList.addTopLevelItem(ViewNode)
self.FilterList.addTopLevelItem(DifficultyNode)
self.FilterList.addTopLevelItem(TopicNode)
self.FilterList.addTopLevelItem(TypeNode)
EasyNode = QTreeWidgetItem(["Easy"])
EasyNode.setCheckState(0, Qt.Unchecked)
NormalNode = QTreeWidgetItem(["Normal"])
NormalNode.setCheckState(0, Qt.Unchecked)
HardNode = QTreeWidgetItem(["Hard"])
HardNode.setCheckState(0, Qt.Unchecked)
MultipleNode = QTreeWidgetItem(["Multiple Choice"])
MultipleNode.setCheckState(0, Qt.Unchecked)
ShortNode = QTreeWidgetItem(["Short Answer"])
ShortNode.setCheckState(0, Qt.Unchecked)
NumericNode = QTreeWidgetItem(["Numerical Answer"])
NumericNode.setCheckState(0, Qt.Unchecked)
Topic1_Node = QTreeWidgetItem(["Multiples, Factors, Primer Numbers and Prime Factors"])
Topic1_Node.setCheckState(0, Qt.Unchecked)
Topic2_Node = QTreeWidgetItem(["Fractions (+-x and ÷) non-calculator approach"])
Topic2_Node.setCheckState(0, Qt.Unchecked)
Topic3_Node = QTreeWidgetItem(["Significant Figures, Decimal Places"])
Topic3_Node.setCheckState(0, Qt.Unchecked)
Topic4_Node = QTreeWidgetItem(["Scatter Graphs and Correlation"])
Topic4_Node.setCheckState(0, Qt.Unchecked)
Topic5_Node = QTreeWidgetItem(["Directed Numbers and Coordinates"])
Topic5_Node.setCheckState(0, Qt.Unchecked)
Topic6_Node = QTreeWidgetItem(["Representing Numbers by letters/Algebra"])
Topic6_Node.setCheckState(0, Qt.Unchecked)
Topic7_Node = QTreeWidgetItem(["Circle Circumference/Area"])
Topic7_Node.setCheckState(0, Qt.Unchecked)
Topic8_Node = QTreeWidgetItem(["Pie Charts and Averages"])
Topic8_Node.setCheckState(0, Qt.Unchecked)
Topic9_Node = QTreeWidgetItem(["Angles"])
Topic9_Node.setCheckState(0, Qt.Unchecked)
Topic10_Node = QTreeWidgetItem(["Breaking Brackets"])
Topic10_Node.setCheckState(0, Qt.Unchecked)
Topic11_Node = QTreeWidgetItem(["Percentages"])
Topic11_Node.setCheckState(0, Qt.Unchecked)
Topic12_Node = QTreeWidgetItem(["Measuring - Length, area, volume, weight, metric and imperial units"])
Topic12_Node.setCheckState(0, Qt.Unchecked)
Topic13_Node = QTreeWidgetItem(["Triangles"])
Topic13_Node.setCheckState(0, Qt.Unchecked)
Topic14_Node = QTreeWidgetItem(["Equations"])
Topic14_Node.setCheckState(0, Qt.Unchecked)
Topic15_Node = QTreeWidgetItem(["Ratio"])
Topic15_Node.setCheckState(0, Qt.Unchecked)
TopicNode.addChild(Topic1_Node)
TopicNode.addChild(Topic2_Node)
TopicNode.addChild(Topic3_Node)
TopicNode.addChild(Topic4_Node)
TopicNode.addChild(Topic5_Node)
TopicNode.addChild(Topic6_Node)
TopicNode.addChild(Topic7_Node)
TopicNode.addChild(Topic8_Node)
TopicNode.addChild(Topic9_Node)
TopicNode.addChild(Topic10_Node)
TopicNode.addChild(Topic11_Node)
TopicNode.addChild(Topic12_Node)
TopicNode.addChild(Topic13_Node)
TopicNode.addChild(Topic14_Node)
TopicNode.addChild(Topic15_Node)
DifficultyNode.addChild(EasyNode)
DifficultyNode.addChild(NormalNode)
DifficultyNode.addChild(HardNode)
TypeNode.addChild(MultipleNode)
TypeNode.addChild(ShortNode)
TypeNode.addChild(NumericNode)
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
main()
将插槽连接到树小部件的 itemChanged 信号,如下所示:
self.FilterList.itemChanged.connect(self.handleItemChanged)
...
def handleItemChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print('Item Checked')
elif item.checkState(column) == QtCore.Qt.Unchecked:
print('Item Unchecked')