2013-04-02 46 views
0

我有一個包含從那裏我需要打電話的場景在小部件的場景和按鈕返回主窗口:Qt和蟒蛇 - 如何引用另一個類

import sys 
from PySide.QtCore import * 
from PySide.QtGui import * 

class Widget(QWidget): 
    def __init__(self, scene): 
     super(Widget, self).__init__() 

     self.refreshButton = QPushButton("Refresh", self) 
     self.refreshButton.clicked.connect(self.Refresh) 
       # THIS ACTION SHOULD PROCEED ARGUMENTS 
       # TO FUNCION "Refresh" 
     layout = QHBoxLayout() 
     layout.addWidget(self.refreshButton) 

     self.setLayout(layout) 
     self.show() 

    def Refresh(self, scene): 
     mainWinScene = scene 
     print "On Refresh! - ", mainWinScene.items() 

class MainScene(QGraphicsScene): 
    def __init__(self): 
     super(MainScene, self).__init__() 


class MainWindow(QMainWindow): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 

     self.scene = MainScene() 
     self.scene.setSceneRect(0,0,200,100) 
     self.scene.addLine(20,10,150,80) 

     self.view = QGraphicsView() 
     self.view.setScene(self.scene) 

     drawRectAct = QAction('&Add Rectangle', self) 
     drawRectAct.triggered.connect(self.drawRect) 
     shapeInspectorAct = QAction('&Show Inspector', self) 
     shapeInspectorAct.triggered.connect(self.showInspector) 

     menubar = self.menuBar() 
     fileMenu = menubar.addMenu('&Shapes') 
     fileMenu.addAction(drawRectAct) 
     fileMenu.addAction(shapeInspectorAct) 

     self.setCentralWidget(self.view) 

    def drawRect(self): 
     self.scene.addRect(50,50,20,30) 

    def showInspector(self): 
     self.I = Widget(self.scene) 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    main = MainWindow() 
    main.show() 
    sys.exit(app.exec_()) 

如何進行「現場」的說法與行動 - 以「刷新」功能?

回答

1

可以在Widget的構造函數傳遞一個場景:

class Widget(QWidget): 
    def __init__(self, scene): 
     ... 
     self.scene = scene 
     ... 

    def Refresh(self): 
     print "On Refresh! - ", self.scene.items() 


class MainWindow(QMainWindow): 
... 
    def showInspector(self): 
     self.I = Widget(self.scene) 
... 
+0

+1現在。這應該是很好的工作,但在嘗試測試時,我遇到了另一個問題 - 「def Refresh(self,scene):」現在有需要通過槽操作的參數「self.refreshButton.clicked.connect(self.Refresh )」。這是QAction必須將參數傳遞給調用函數,請幫助。 – Alex

+1

@Alex更新答案,以便您可以抓住它。 – graphite

+0

我可能會引用Widget .__ init__中的場景,但採取行動進行參數將是正確的解決方案。 – Alex