2010-11-11 11 views
5

Estoy intentando conectarme a la señal selectionChanged de un QTreeView usando PyQt. He hecho esto en el pasado (para un QTableView) y tuve éxito. Pero ahora no puedo hacer que un código similar funcione.PyQt QTreeView: intentando conectar con la señal de selección de cambio

En el siguiente ejemplo de código, me conecto con éxito a las señales expandidas y contraídas, pero no a la selección Señales cambiadas o activadas. ¿Podría alguien decirme qué estoy haciendo mal? Gracias.

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" 

Respuesta

13

Ok, lo descubrí (mayormente por accidente).

Como estaba haciendo las conexiones en el init pero solo establecí el modelo para este QTreeView más adelante, no había un modelo de selección válido en su lugar.

Con el fin de hacer más trabajo que tenía que hacer dos cambios:

1) El objeto que emite tuvo que ser cambiado para ser SelectionModel del QTreeView. No sé por qué, pero algunos (raros) ejemplos en la web sugirió que esto podría ser el caso

y

2) que tenía que reemplazar el método setModel del QTreeView tal que llama a la superclase 'Método setModel y luego hace las conexiones después.

Así que el nuevo código es el siguiente:

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" 
3

Si está utilizando declarativa que puede hacer algo como:

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