[PyQt] selecting items from a treeview using code

Wim Verhavert wim.verhavert at gmail.com
Tue Sep 9 21:11:43 BST 2008


Sounds easy he, but this one is really driving me crazy...

So here's what I've done so far.

Ok: I have a QTreeview (using QT Designer by the way) filled with
objects using my own homebrew objectTableModel(QAbstractTableModel).
This model stores POPO's and allows me to retrieve them. So far no
problem. Now I want to highlight + select the last item of the tree
(using code).
So here's what I've done:
-  I created an instance of objectTableModel for storing my invoices
-> invoiceModel
-  I created a method getIndexOf(self, object) in my objectTableModel
class which returns a QModelIndex instance. Then I do:

last = self.invoiceModel.getIndexOf(self.invoices[-1])
self.treeInvoices.selectionModel().select(last, QItemSelectionModel.Rows)

But this doesn't do anything.

I also tried:
last = self.invoiceModel.getIndexOf(self.invoices[-1])
self.treeInvoices.setSelection(self.treeInvoices.visualRect(last),
QItemSelectionModel.Rows)

But this also doesn't do anything...

Am I just too blind to see the thing here? This is keeping me up for
hours now...

Any help greatly appreciated...

Oh yeah: I'm on windows here (XP pro)


More information about the PyQt mailing list