[PyQt] newbie: trying to place widgets in a QTreeWidget

blue_riviera imre.tuske at gmail.com
Tue Aug 18 16:37:55 BST 2009


I'm new to both Python and PyQt and still learning the ropes (familiar with
C++, though). What I'm trying to do is to create a QTreeWidget where some
items are widgets (say buttons for this example's sake).

When I try this code (snippet):

			cw = QTreeWidget()

			for n in range(3):

				i = QTreeWidgetItem(cw) # new row
				i.setText(0, "first "+str(n)) # text in first column
				i.setText(1, "second") # text in second column

				b = QPushButton("push me "+str(n)) # button...
				cw.setItemWidget(i, 2, b) # ...goes to the third column

...the result is that I'll have 3 rows in my tree widget, but only in the
third row will have a button (all three will have the texts).

However when I append the following code to the above snippet:

			b1 = QPushButton("push me 0")
			cw.setItemWidget(cw.topLevelItem(0), 2, b1) # add button to row 0, column

			b2 = QPushButton("push me 1")
			cw.setItemWidget(cw.topLevelItem(1), 2, b2) # add button to row 1, column

...which is basically a loop unrolled (but the created buttons are referred
to by two separate variables), all two buttons will appear.

So my suspicion is that it is not a Qt issue, rather a widget ownership one
(I suppose when a new button is assigned to the 'b' variable in the loop,
the previous button loses 'b' as owner and it gets destroyed).

I suppose I should somehow transfer the button's ownership to (let's say)
the tree widget once it is created but I don't know how to do that. Anyone
can help me out?


View this message in context: http://www.nabble.com/newbie%3A-trying-to-place-widgets-in-a-QTreeWidget-tp25027612p25027612.html
Sent from the PyQt mailing list archive at Nabble.com.

More information about the PyQt mailing list