[PyQt] Signal on Selection in QTreeView

Kevin Foss fossks at cox.net
Wed Mar 19 00:54:44 GMT 2008


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.

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>
1 items selected
1 items deselected
3
0
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
1
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
2
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
1
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
0
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
1
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
2
<PyQt4.QtCore.QVariant object at 0xb7a75b2c>
1 items selected
1 items deselected
3
1


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.

If I have the "selected" information, what do I need to do to this to get the 
QVariant out?  Or how does this become by INDEX?

Ideas?

Kevin

On Tuesday 18 March 2008 02:48:48 pm Andreas Pakulat wrote:
> On 18.03.08 13:37:46, fossks at cox.net wrote:
> > I have been successful in getting the row number back to me for a
> > selected row.  I have found that the returned row number is not
> > consistent with the list but with the view.  Therefore row '0' when I
> > start is row '10' on a 10 item list when the view is sorted in the
> > opposite direction.
> >
> > Is there a simple method of retrieving the item data out of the list
> > model for the selected item?
>
> You seriously need to read the documentation on the model/view framework
> in Qt. http://doc.trolltech.com/4.3/model-view-programming.html
>
> In short: You can retrieve the model indexes from the selection, with
> that index you can ask the model itself for the data of that index via
> the model's data() function. That function takes an index, a role
> (usually you want Qt.DisplayRole here, but it may also need a custom
> role, that depends on the implementation of the model). This will return
> a QVariant carrying the data, you just need to get out the actual data
> (string, integer, list and so on) and can then populate the custom
> widget.
>
> Andreas




More information about the PyQt mailing list