PySide:将 QItemSelectionModel 与 QListView 一起使用时的 Segfault(?)

2022-01-12 00:00:00 python pyside qt segmentation-fault crash

问题描述

与此完全相同的问题:连接 QTableView selectionChanged 信号会产生段错误PyQt

我有一个QListView,我想在选择一个项目时调用一个函数:

I have a QListView, and I want to call a function when an item 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)

但是,当它到达我正在使用选择模型的最后一行时,应用程序崩溃了.不是回溯,而是来自 Windows 的应用程序名称已停止工作".我很确定这是一个段错误.

But, when it reaches the last line, where I'm using the selection model, the app crashes. Not with a traceback, but with a "appname has stopped working" from Windows. I'm pretty sure that's a segfault.

但是,当我使用 PyQt4 时,它工作正常.我使用 PySide 因为它是 LGPL.

BUT, when I use PyQt4 it works fine. I'm using PySide because it's LGPL.

是的,我使用的是最新版本(PySide:1.2.1、Python 2.7.5、Qt 4.8.5).

Yes, I'm on the latest versions of everything (PySide: 1.2.1, Python 2.7.5, Qt 4.8.5).

谁能帮我解决这个问题?

Can anyone help me with this?


解决方案

尝试在选择模型的生命周期内保持对选择模型的引用.这对我来说有类似的问题(连接到表视图选择模型上的 currentChanged 事件时出现段错误).

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.

相关文章