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)



  • Hi,

    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

    I have

    view -> model -> mastermodel -> logic -> entry

    with types

    • 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.model.setSourceModel(self.mastermodel)
    ....
    self.view = QTreeView()
    self.view.setModel(self.model)

    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)
    self.selectionModel.select(selection, QItemSelectionModel.ClearAndSelect)
    self.view.scrollTo(c_index)
    self.view.setCurrentIndex(c_index)

    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!


  • Lifetime Qt Champion

    Hi,

    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



  • @SGaist said in Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5):

    mapFromSource

    Thanks for your answer!
    While I was so sure, I had tried it already, obviously I had not.... It now works.!

    Thanks a lot!!


Log in to reply