[PyQt] Signal on Selection in QTreeView

Andreas Pakulat apaku at gmx.de
Wed Mar 19 07:48:23 GMT 2008

On 18.03.08 17:54:44, Kevin Foss wrote:
> I really hate to keep coming back for help like this but even after re-reading 
> the reference you gave (I've read it before), I still have yet to  understand 
> what constitutes an INDEX.

A QModelIndex is simply a little helper class that stores all data to
uniquely identify 1 "element" in the model, that includes a row and
column number and the model itself. For tree-like models the index often
also carries some internal data that the model uses to create the

> In the case of the following, I've gotten the selected row from the call to 
> SLOT.  I have read through the definition of an INDEX and tried to file it in 
> with last line. 
>     def slot(self, selected, deselected): 
> 	print len(selected), "items selected" 
> 	print len(deselected), "items deselected" 
> 	print self.tm.rowCount()
> 	if len(selected) != 0: 
> 		rownr = selected[0].topLeft().row() 
> 		print rownr
> 		print self.tm.data(self.tm.index(rownr, 0, QModelIndex()), Qt.DisplayRole)
>  However when I select an item I get :
> 1 items selected
> 1 items deselected
> 3
> 1
> <PyQt4.QtCore.QVariant object at 0xb7a75b2c>

Good, so everythings working as expected.

> This tells me that the returned QVariant you mention in your response is my 
> data.  I haven't figured out how to retrieve this yet, however, if I remember 
> my earlier programming days, that this is a pointer and if the pointer is not 
> changing, I'm not pointing at different data.

There are no pointers in Python. You deal just with object references.
The above is a QVariant, which is just a data-container and can contain
any kind of basic data (numbers, strings, lists, maps and a few others).
There are specific access methods to get that data out (toXXX), however
each of them will only work if the encapsulated data is convertable to
that type (i.e. using toString() on a QVariant that has a list in it
won't work). So thats the point where you need to look into the source
or API docs of your model and check what kind of data is returned for
the first column of each row for the display role. 


You could live a better life, if you had a better mind and a better body.

More information about the PyQt mailing list