Alex
Alex

Reputation: 3267

Qt and python - how to refer to another class

I have main window which contains scene and button in widget from where I need to call scene:

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

How to proceed "scene" argument with action - to "Refresh" function?

Upvotes: 0

Views: 392

Answers (1)

graphite
graphite

Reputation: 2960

You can pass a scene in Widget's constructor:

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)
 ...

Upvotes: 1

Related Questions