我需要写一棵树?在PyQt的。 它看起来像这样:
Clients(this is text)
Type A (this is a Clients child and has a checkbox)
Type B (this is a Clients child and has a checkbox)
Vendors(this is text)
Mary (this is a Vendors child and has a checkbox)
Arnold (this is a Vendors child and has a checkbox)
Time Period
Init(this is a Time Period child, and would be a calendarWidget for date selection)
End (this is a Time Period child, and would be a calendarWidget for date selection)
你会推荐这算什么? QTreeWidget
? QTreeView?
这将是我将用它来建立SQL查询点击的项目。
谢谢阅读。
我建议你使用QTreeWidget代替QTreeView则 ,因为你的任务是非常简单的。 QTreeView则 (含定制机型,例如QStandardItemModel )是困难的事件。 你是简单的。
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.treeWidget = QtGui.QTreeWidget()
self.treeWidget.setHeaderHidden(True)
self.addItems(self.treeWidget.invisibleRootItem())
self.treeWidget.itemChanged.connect (self.handleChanged)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.treeWidget)
self.setLayout(layout)
def addItems(self, parent):
column = 0
clients_item = self.addParent(parent, column, 'Clients', 'data Clients')
vendors_item = self.addParent(parent, column, 'Vendors', 'data Vendors')
time_period_item = self.addParent(parent, column, 'Time Period', 'data Time Period')
self.addChild(clients_item, column, 'Type A', 'data Type A')
self.addChild(clients_item, column, 'Type B', 'data Type B')
self.addChild(vendors_item, column, 'Mary', 'data Mary')
self.addChild(vendors_item, column, 'Arnold', 'data Arnold')
self.addChild(time_period_item, column, 'Init', 'data Init')
self.addChild(time_period_item, column, 'End', 'data End')
def addParent(self, parent, column, title, data):
item = QtGui.QTreeWidgetItem(parent, [title])
item.setData(column, QtCore.Qt.UserRole, data)
item.setChildIndicatorPolicy(QtGui.QTreeWidgetItem.ShowIndicator)
item.setExpanded (True)
return item
def addChild(self, parent, column, title, data):
item = QtGui.QTreeWidgetItem(parent, [title])
item.setData(column, QtCore.Qt.UserRole, data)
item.setCheckState (column, QtCore.Qt.Unchecked)
return item
def handleChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print "checked", item, item.text(column)
if item.checkState(column) == QtCore.Qt.Unchecked:
print "unchecked", item, item.text(column)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())