我做了一个函数,允许添加项目到树(父+子或子)。
def addItemsToSummary(self, parent, text, checkable=False, expanded=True):
self.tabItems = []
self.tabParent.append(text)
self.item = QTreeWidgetItem(parent, [text])
lhs, rhs = self.item.text(0).split("=", 1)
self.nominalValue,_ = rhs.split(")", 1)
print(self.nominalValue)
self.val = re.findall('\w+', self.nominalValue)
print(self.val)
print(self.val[0])
print(self.val[1])
if checkable:
self.item.setCheckState(0, Qt.Unchecked)
else:
self.item.setFlags(self.item.flags() & ~Qt.ItemIsUserCheckable)
self.item.setExpanded(expanded)
self.addChildToSummary(self.item, ["Nominal value = ", self.val[0], self.val[1]])
self.addChildToSummary(self.item, ["Min =","",self.val[1]])
self.addChildToSummary(self.item, ["Max =","",self.val[1]])
self.addChildToSummary(self.item, ["Type = ", "float",''])
self.addChildToSummary(self.item, ["Relation =","",""])
if self.tabItems is not None:
self.tabAnalysis.append(self.tabItems)
return self.tabAnalysis, self.nominalValue
我做了另一个函数,允许将每个子项添加到一个项(父项)中。
def addChildToSummary(self, parent, tab):
item1 = QTreeWidgetItem(parent, [tab[0]])
item2 = QTreeWidgetItem(parent, [tab[1]])
item3 = QTreeWidgetItem(parent, [tab[2]])
item2.setFlags(item2.flags() | Qt.ItemIsEditable)
self.tabChild.append((item1.text(0),item2.text(0),item3.text(0)))
print(item2.text(0))
self.tabItems.append([parent.text(0),(item1.text(0),item2.text(0),item3.text(0))])
return item1, item2, self.tabItems
我想知道是否可以将每个
QTreeWidgetItem
放入QTreeWidget
列。 最佳答案
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow,self).__init__(parent)
self.initUI()
def createGroup(self):
groupBox = QGroupBox()
self.treeWidget = QTreeWidget()
header=QTreeWidgetItem(["Tree","First","secondo"])
#...
self.treeWidget.setHeaderItem(header) #Another alternative is setHeaderLabels(["Tree","First",...])
root = QTreeWidgetItem(self.treeWidget, ["root"])
A = QTreeWidgetItem(root, ["A"])
barA = QTreeWidgetItem(A, ["bar", "i", "ii"])
bazA = QTreeWidgetItem(A, ["baz", "a", "b"])
barA.setFlags(barA.flags() | Qt.ItemIsEditable)
bazA.setFlags(bazA.flags() | Qt.ItemIsEditable)
# switch off "default" editing behaviour
# as it does not allow to configure only an individual
# column as editable
self.treeWidget.setEditTriggers(self.treeWidget.NoEditTriggers)
# to be able to decide on your own whether a particular item
# can be edited, connect e.g. to itemDoubleClicked
self.treeWidget.itemDoubleClicked.connect(self.checkEdit)
vbox = QVBoxLayout()
vbox.addWidget(self.treeWidget)
vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
# in your connected slot, you can implement any edit-or-not-logic
# you want
def checkEdit(self, item, column):
# e.g. to allow editing only of column 1:
if column == 1:
self.treeWidget.editItem(item, column)
def initUI(self):
self.resize(300, 220)
self.grid = QGridLayout()
self.widget = QWidget()
self.widget.setLayout(self.grid)
self.setCentralWidget(self.widget)
self.grid.addWidget(self.createGroup(),1,0,1,2)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create("Fusion"))
form = MainWindow()
form.show()
sys.exit(app.exec_())