Multiple Columns in PyQt4 (potentially using QTreeWidget)

浪尽此生 提交于 2019-12-09 06:44:52

问题


I'm trying to get QTreeWidget working exactly similar to this one. In python! I don't care about multiple tabs but about multiple columns.

This is what I've got so far. I don't know how to have more than one header though.

self.pointListBox = QtGui.QTreeWidget()

x=QtGui.QTreeWidgetItem()
x.setText(0,str(coords[0]))
y=QtGui.QTreeWidgetItem()
y.setText(0,str(coords[1]))

Qname=QtGui.QTreeWidgetItem()
Qname.setText(0,new_point_name)

self.pointListBox.setHeaderItem(Qname)
parent = QtGui.QTreeWidgetItem(self.pointListBox)
parent.setText(0,new_point_name)
parent.addChild(x)
parent.addChild(y)

回答1:


There's a few things you'll want to fix there.

from PyQt4 import QtCore, QtGui
import sys

app = QtGui.QApplication(sys.argv)
QtGui.qApp = app

pointListBox = QtGui.QTreeWidget()

header=QtGui.QTreeWidgetItem(["Tree","First","secondo"])
#...
pointListBox.setHeaderItem(header)   #Another alternative is setHeaderLabels(["Tree","First",...])

root = QtGui.QTreeWidgetItem(pointListBox, ["root"])
A = QtGui.QTreeWidgetItem(root, ["A"])
barA = QtGui.QTreeWidgetItem(A, ["bar", "i", "ii"])
bazA = QtGui.QTreeWidgetItem(A, ["baz", "a", "b"])


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

I didn't finish the example, but that should get you reasonably close.

Note that instead of barA = QtGui.QTreeWidgetItem(A, ["bar", "i", "ii"]), there's nothing wrong with

barA = QtGui.QTreeWidgetItem(A)
barA.setText(0,"bar")
barA.setText(1,"i")
barA.setText(2,"ii")

if you need to calculate something before displaying the text.



来源:https://stackoverflow.com/questions/11980149/multiple-columns-in-pyqt4-potentially-using-qtreewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!