Python PySide:Segfault(?)在QListView中使用QItemSelectionModel时

Python PySide:Segfault(?)在QListView中使用QItemSelectionModel时,python,qt,crash,segmentation-fault,pyside,Python,Qt,Crash,Segmentation Fault,Pyside,与此完全相同的问题: 我有一个QListView,我想在选择一个项目时调用一个函数: 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.c

与此完全相同的问题:

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

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中的“appname已停止工作”。我敢肯定那是个错误

但是,当我使用PyQt4时,它工作得很好。我使用PySide是因为它是LGPL

是的,我使用的是所有东西的最新版本(PySide:1.2.1、python2.7.5、qt4.8.5)


有人能帮我吗?

尝试在选择模型的生命周期中保留对选择模型的引用。这对我来说也有类似的问题(连接到表视图选择模型上的currentChanged事件时出现seg故障)

出于某种原因,最后两行可以工作,而将它们合并到一个命令中会抛出错误

这里也有同样的问题:

我还回答说:

我怀疑发生的是:

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!

这对我也有用。(我在最后一行中使用了self.server\u list\u selection\u model,而不是self.server\u list.selectionModel()),但是为什么这可以工作,问题中的代码不能工作呢?你是对的,最后一行应该是self.server\u list\u selectionModel。我会纠正的。我自己也不太明白,但这个问题是由qt与pyside绑定交互(或者说不交互)的引用计数垃圾收集实现引起的。如果您不想保留对选择模型的引用,可以使用老式的信号连接语法:
QtCore.QObject.connect(self.server\u list.selectionModel(),QtCore.SIGNAL(“currentChanged(QModelIndex,QModelIndex)”),self.server\u changed)
。我不知道在保留选择模型时是否存在任何陷阱。但我更喜欢编写
selectionChanged.connect(self.server\u changed)
,就像你一样。
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!