[PyQt] Easy Question - How to use selectionChanged on a QTreeView?

Richie Ward richies at gmail.com
Sun Nov 20 22:27:13 GMT 2016


Hi, I am a bit confused as to how to use the selectionChanged event on
a QTreeView.

As I understand it, I have to use
setSelectionModel(MySelectionModel()) and subclass
QItemSelectionModel. I can then override selectionChanged. My attempts
to do this have resulted in this error:
QAbstractItemView::setSelectionModel() failed: Trying to set a
selection model, which works on a different model than the view.

I am using declarative UI in my program and I load it with
uic.loadUi(ui_path, self).

I was hoping to code it like this but I don't think its this simple :-(

class SelectionModel(QItemSelectionModel):
    def selectionChanged(self, old_selection, new_selection):
        print("Selection changed in model")
...

self.ui = uic.loadUi(main_path, self)
self.ui.treeGame.setSelectionModel(SelectionModel())

I did try googling but I still cant figure it out so I am asking here.

-- 
Thanks, Richie Ward
https://uk.linkedin.com/in/richie-ward-07ab0495


More information about the PyQt mailing list