4
votes

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.

image

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

1 Answers

19
votes

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.