0

我有主窗口,其中包含小部件中的场景和按钮,我需要从中调用场景:

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_())

如何通过动作进行“场景”参数 - 到“刷新”功能?

4

1 回答 1

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)
 ...
于 2013-04-02T06:53:03.347 回答