我正在尝试使用 PyQt 连接到 QTreeView 的 selectionChanged 信号。我过去曾这样做过(对于 QTableView)并且成功了。但现在我无法让类似的代码工作。
在以下代码示例中,我成功连接到展开和折叠的信号,但没有连接到 selectionChanged 或激活的信号。有人可以告诉我我做错了什么吗?谢谢。
from PyQt4 import QtGui
from PyQt4 import QtCore
################################################################################
class ShaderDefTreeView(QtGui.QTreeView):
"""
Overrides the QTreeView to handle keypress events.
"""
#---------------------------------------------------------------------------
def __init__(self, parent=None):
"""
Constructor for the ShaderDefTreeView class.
"""
super(ShaderDefTreeView, self).__init__(parent)
#listen to the selectionChanged signal
print "Connecting"
#whenever the selection changes, let the data model know
self.connect(self,
QtCore.SIGNAL("selectionChanged(QItemSelection&, QItemSelection&)"),
self.store_current_selection)
self.connect(self, QtCore.SIGNAL("activated(const QModelIndex &)"),
self.activated)
self.connect(self, QtCore.SIGNAL("collapsed(const QModelIndex &)"),
self.collapsed)
self.connect(self, QtCore.SIGNAL("expanded(const QModelIndex &)"),
self.expanded)
#---------------------------------------------------------------------------
def store_current_selection(self, newSelection, oldSelection):
print "changed"
#self.model().selection_changed(newSelection)
#---------------------------------------------------------------------------
def expanded(self, newSelection):
print "expanded"
#---------------------------------------------------------------------------
def collapsed(self, newSelection):
print "collapsed"
#---------------------------------------------------------------------------
def activated(self, newSelection):
print "activated"