Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5)
Forum Updated to NodeBB v4.3 + New Features

Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5)

Scheduled Pinned Locked Moved Solved Language Bindings
3 Posts 2 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hermes
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • H Offline
        H Offline
        hermes
        wrote on last edited by
        #3

        @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!!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved