2010-11-11 6 views
5

मैं चयन से कनेक्ट करने का प्रयास कर रहा हूं PyQt का उपयोग कर QTreeView का चेंग्ड सिग्नल। मैंने इसे अतीत में किया है (एक QTableView के लिए) और सफल रहा। लेकिन अब मुझे काम करने के लिए समान कोड नहीं मिल सकता है।पीईक्यूटी क्यू ट्री व्यू: चयन से कनेक्ट करने का प्रयास किया गया चेंज सिग्नल

निम्नलिखित कोड उदाहरण में, मैं विस्तारित और ध्वस्त संकेतों से सफलतापूर्वक कनेक्ट हूं, लेकिन चयन के लिए चयनित या सक्रिय सिग्नल नहीं। क्या कोई मुझे बता सकता है कि मैं क्या गलत कर रहा हूं? धन्यवाद।

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" 

उत्तर

13

ठीक है, इसे समझ लिया (ज्यादातर दुर्घटना से)।

चूंकि मैं init में कनेक्शन बना रहा था, लेकिन बाद में केवल इस QTreeView के लिए मॉडल सेट कर रहा था, वहां कोई वैध चयन मॉडल नहीं था।

1) उत्सर्जक वस्तु QTreeView के selectionModel होने के लिए परिवर्तित किया जा सकता था:

आदेश यह काम करने के लिए मैं दो परिवर्तन करने के लिए किया था। मैं पता नहीं क्यों, लेकिन कुछ (दुर्लभ) वेब पर उदाहरण सुझाव दिया है कि क्या ऐसा मामला है

और

2) मैं QTreeView इस तरह की 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" 
3

आप कथात्मक उपयोग कर रहे हैं आप की तरह कुछ कर सकते हैं:

self.ui = uic.loadUi(main_path, self) 
self.ui.tree.selectionModel().selectionChanged.connect(self.item_selection_changed_slot) 
संबंधित मुद्दे