PySide: Segfault(?) when using QItemSelectionModel with QListView

后端 未结 2 1883
野性不改
野性不改 2021-01-04 22:59

Same exact problem as this: Connecting QTableView selectionChanged signal produces segfault with PyQt

I have a QListView, and I want to call a function when an item

相关标签:
2条回答
  • 2021-01-04 23:23

    Try holding a reference to the selection model for the lifetime of the selection model. That worked for me with a similar problem (seg fault when connecting to currentChanged event on a table views selection model).

    self.server_list = QtGui.QListView(self.main_widget)
    self.server_list_model = QtGui.QStandardItemModel()
    self.server_list.setModel(self.server_list_model)
    self.server_list_selection_model = self.server_list.selectionModel() # workaround
    self.server_list_selection_model.selectionChanged.connect(self.server_changed)
    

    For some reason, the last two lines work, while combining them into one command throws an error.

    0 讨论(0)
  • 2021-01-04 23:23

    Same problem here: http://permalink.gmane.org/gmane.comp.lib.qt.pyside.devel/541

    And I also answered: http://permalink.gmane.org/gmane.comp.lib.qt.pyside.devel/542

    I suspect what happens is:

    self.server_list # local object
    .selectionModel() # call C++ method, wraps C++ object in Python object
    .selectionChanged # get property of object
    # selection model is now out of scope and gets garbage collected
    .connect(...) # OOPS! ...operating on object that no longer exists!
    
    0 讨论(0)
提交回复
热议问题