【问题标题】:PyQt QTreeView: Trying to connect to the selectionChanged signalPyQt QTreeView:尝试连接到 selectionChanged 信号
【发布时间】:2011-05-08 18:56:14
【问题描述】:

我正在尝试使用 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"

【问题讨论】:

    标签: qt pyqt signals signals-slots qtreeview


    【解决方案1】:

    好的,想通了(大部分是偶然的)。

    由于我是在 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"
    

    【讨论】:

      【解决方案2】:

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

      self.ui = uic.loadUi(main_path, self)
      self.ui.tree.selectionModel().selectionChanged.connect(self.item_selection_changed_slot)
      

      【讨论】:

        猜你喜欢
        • 2016-11-13
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 2016-11-03
        • 2013-07-14
        • 1970-01-01
        相关资源
        最近更新 更多