0

I would like to plot the data in ctrl function in a figure that is created in Plot class.

class Data(QWidget):

  def __init__(self):
    QWidget.__init__(self)
    self.layout = QHBoxLayout()
    button_widget = QWidget()
    button_layout = QVBoxLayout()
    self.btn_off = QPushButton("Off")
    self.btn_off.clicked.connect(self.ctrl)
    button_widget.setLayout(button_layout)
    self.layout.addWidget(button_widget, stretch=1)
    self.setLayout(self.layout)

  def ctrl(self):
    graphWidget = Plot().graphWidget
    y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    x = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
    graphWidget.plot(x, y)

class Plot(QWidget):
  def __init__(self):
    QWidget.__init__(self)
    self.layout = QVBoxLayout()
    button_widget = QWidget()
    button_layout = QVBoxLayout()
    ly_widget = QWidget()
    ly = QVBoxLayout()
    self.graph()
    ly.addWidget(self.graphWidget)
    ly_widget.setLayout(ly)
    self.layout.addWidget(ly_widget)
    widget2 = Data()
    self.layout.addWidget(widget2, stretch=4)
    self.setLayout(self.layout)


  def graph(self):
    self.graphWidget = pg.PlotWidget()


class MainWidget(QWidget):
  def __init__(self):
    QWidget.__init__(self)
    self.layout = QVBoxLayout()
    bottom_widget = Plot()
    self.layout.addWidget(bottom_widget, stretch=10)
    self.setLayout(self.layout)


class Window(QMainWindow):

  def __init__(self, widget):
    QMainWindow.__init__(self)
    self.setCentralWidget(widget)
    widget.parent = self

app = QApplication(sys.argv)
main_widget = MainWidget()
win = Window(main_widget)
win.show()
sys.exit(app.exec_())

But graphWidget is not recognized in Data class. Can someone help me with either sending the data to the graph function and plotting them or plotting them inside the ctrl function?

1 Answer 1

0

You have some serious problems with your code.

First of all, you are trying to create a new Plot instance, which doesn't make any sense, since one already exists and you need to access that.

Doing it like that also creates another important problem: you're just keeping a local reference to the graph widget, not its parent Plot instance; this results in that Plot being immediately destroyed, and since the graph widget is reparented to it (after being added to the layout), it gets immediately destroyed as well.

There are other issues too:

  • you never add the button to the layout;
  • you create unnecessary widgets and layouts;
  • you're trying to overwrite parent of the MainWidget, but parent() is an existing function of all QObjects, and shall never be overwritten;

A better approach to do what you want is to connect to the button from the Plot instance, or, eventually use a custom signal:

class Data(QWidget):
    dataSignal = pyqtSignal(object, object)
    def __init__(self):
        QWidget.__init__(self)
        layout = QHBoxLayout(self)
        self.btn_off = QPushButton("Off")
        layout.addWidget(self.btn_off)
        self.btn_off.clicked.connect(self.ctrl)

    def ctrl(self):
        y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        x = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
        self.dataSignal.emit(x, y)

class Plot(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QVBoxLayout(self)
        self.graphWidget = pg.PlotWidget()
        layout.addWidget(self.graphWidget)
        widget2 = Data()
        layout.addWidget(widget2, stretch=4)

        widget2.dataSignal.connect(self.graphWidget.plot)


class MainWidget(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.layout = QVBoxLayout()
        bottom_widget = Plot()
        self.layout.addWidget(bottom_widget, stretch=10)
        self.setLayout(self.layout)


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        widget = MainWidget()
        self.setCentralWidget(widget)

import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. How can I plot just one point i.e. having one constant value for x and y instead of a list? @musicamante

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.