PyQt QTreeView:尝试连接到selectionChanged信号

bvz*_*bvz 5 qt signals pyqt signals-slots qtreeview

我正在尝试使用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"
Run Code Online (Sandbox Code Playgroud)

bvz*_*bvz 13

好吧,想通了(大多是偶然的).

由于我在init中建立连接但仅稍后为此QTreeView设置模型,因此没有有效的selectionModel.

为了使它工作,我不得不做两个改变:

1)必须将发射对象更改为QTreeView的selectionModel.我不知道为什么,但网上的一些(罕见的)例子表明情况可能就是这样

2)我必须覆盖QTreeView的setModel方法,以便它调用超类的setModel方法,然后进行连接.

所以新代码看起来像这样:

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)


    #---------------------------------------------------------------------------
    def setModel(self, model):
        super(ShaderDefTreeView, self).setModel(model)
        self.connect(self.selectionModel(),  
                     QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),  
                     self.store_current_selection) 


    #---------------------------------------------------------------------------
    def store_current_selection(self, newSelection, oldSelection):
        print "changed"
Run Code Online (Sandbox Code Playgroud)


小智 6

如果使用声明式,则可以执行以下操作:

self.ui = uic.loadUi(main_path, self)
self.ui.tree.selectionModel().selectionChanged.connect(self.item_selection_changed_slot)
Run Code Online (Sandbox Code Playgroud)