Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5)
hermes last edited by
I'm having a problem with selecting entries in a QTreeview when using the QSortFilterProxyModel using using qt 5.9.5. in python 3 and wonder if someone could point me in the right direction
view -> model -> mastermodel -> logic -> entry
- view: QTreeView()
- model: QSortFilterProxyModel
- mastermodel: custom class called TreeModel (which is inherited from QAbstractItemModel)
- logic: a custom class containing the individual entries
- entry: a custom class containing all info/functions for one entry
The treeview shows multiple colums, showing the various attributes of the entry class instances. I create it with
self.mastermodel = TreeModel(self.logic, mainwindow = self)
self.model = QSortFilterProxyModel()
self.view = QTreeView()
In principle this works nicely, It shows the data and I can delete/add entries etc... Now want to be able to take an entry-entry class instance from my logic class and have QT select the corresponding row (preferably one certain column only) in the treeview. (e.g. after I added an entry, I want to select it)
The selection worked nicely before I added the QSortFilterProxyModel in between. It worked using:
c_index = self.model.createIndex(entry.row(), col, entry)
parent = c_index.parent()
topLeft = self.model.index(entry.row(), col, parent)
bottomRight = self.model.index(entry.row(), col, parent)
selection = QItemSelection(topLeft, bottomRight)
but with the QSortFilterProxyModel I get everything from
"QSortFilterProxyModel: index from wrong model passed to mapFromSource" at the start of the above code to a complete crashes with "Segmentation fault (core dumped)"
Could I explain my problem sufficiently clear? I tried various combinations of mapto/Fromsource but no success.....
Any hints are very much appreciated!
The selection model of your view will be applied to your filter model. From what I remember, you will have to get the indexes of your master model and then using
mapFromSource, the corresponding to your filter model. After that, you should be able to build your selection and apply it.
Hope it helps
hermes last edited by
Thanks for your answer!
While I was so sure, I had tried it already, obviously I had not.... It now works.!
Thanks a lot!!