PySide: Segfault (?) When using QItemSelectionModel with QListView

Exact problem: Connecting a QTableView selectionChanged signal creates a segfault with PyQt

I have a QListView and I want to call a function when an element is selected:

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.selectionModel().selectionChanged.connect(self.server_changed) 

But, when it reaches the last line where I use the selection model, the application crashes. Not with tracing, but with "appname stopped working" from Windows. I am pretty sure segfault.

BUT, when I use PyQt4, it works fine. I use PySide because it is LGPL.

Yes, I am in the latest versions of everything (PySide: 1.2.1, Python 2.7.5, Qt 4.8.5).

Can anyone help me with this?

+8
python segmentation-fault qt crash pyside
source share
2 answers

Try using the selection model link for the lifetime of the selection model. This worked for me with a similar problem (seg error when connecting to the currentChanged event in the table view 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, and combining them into one command causes an error.

+13
source share

Same problem: 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 is happening:

 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! 
+3
source share

All Articles