PyQt QTreeView: Trying to connect to the selectionChanged signal
Asked Answered
B

2

6

I am trying to connect to the selectionChanged signal of a QTreeView using PyQt. I have done this in the past (for a QTableView) and was successful. But now I cannot get similar code to work.

In the following code example, I successfully connect to the expanded and collapsed signals, but not to the selectionChanged or activated signals. Could someone tell me what I am doing wrong? Thanks.

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"
Bel answered 11/11, 2010 at 22:58 Comment(0)
B
14

Ok, figured it out (mostly by accident).

Since I was making the connections in the init but only setting the model for this QTreeView later, there was no valid selectionModel in place.

In order to make it work I had to make two changes:

1) The emitting object had to be changed to be the QTreeView's selectionModel. I don't know why, but some (rare) examples on the web suggested that this might be the case

and

2) I had to override the setModel method of the QTreeView such that it calls the superclass' setModel method and then makes the connections afterwards.

So the new code looks like this:

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"
Bel answered 12/11, 2010 at 0:30 Comment(0)
B
8

If you are using declarative you can do something like:

self.ui = uic.loadUi(main_path, self)
self.ui.tree.selectionModel().selectionChanged.connect(self.item_selection_changed_slot)
Battue answered 20/11, 2016 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.