0

The user enters values using line edits on the MyWidget screen and then presses the Enter button. This opens the MyDialog screen on which data will be plotted when the Run button is pressed. How can I make the line edit data accessible to run in MyDialog for plotting? Or, is there a better way of doing this which wouldn't require passing variables between classes? My program is based on this answer.

from PyQt4 import QtCore, QtGui, uic
#  Import Qt widgets
from matplotlib.backends.backend_qt4agg \
    import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg \
    import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

GUI_FILE = "Minimal_UI.ui"  # GUI with line edit and 'enter' button
form_class = uic.loadUiType(GUI_FILE)[0]


class MyWidget(QtGui.QWidget, form_class):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        self.setupUi(self)
        self.pushButton_Enter.clicked.connect(self.on_pushButton_clicked)
        self.dialogTextBrowser = MyDialog(self)

    @QtCore.pyqtSlot()
    def on_pushButton_clicked(self):
        # I'd like to be able to pass Temp_0 to the run method
        self.Temp_0 = self.lineEdit_Temp_0.text()
        self.dialogTextBrowser.exec_()


class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)

        self.fig = Figure()
        self.canvas = FigureCanvas(self.fig)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.run_button = QtGui.QPushButton('Run')
        self.run_button.clicked.connect(self.run)
        self.stop_button = QtGui.QPushButton('Stop')
        self.stop_button.clicked.connect(self.stop)

        layout = QtGui.QVBoxLayout()
        #  Widgets are stacked in the order they are added
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.run_button)
        layout.addWidget(self.stop_button)

        self.setLayout(layout)

    def run(self):
        # Create axes
        ax = self.fig.add_subplot(111)
        # Discard the old graphs
        ax.hold(False)
        # Plot data--I'd like to be able to use line edit data here
        ax.plot([1, 2, 3, 4], '*-')
        # Refresh canvas
        self.canvas.draw()

    def stop(self):
        print 'Stop Pressed'

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWidget')

    main = MyWidget()
    main.show()

    sys.exit(app.exec_())
Community
  • 1
  • 1
Drew
  • 29
  • 7

1 Answers1

1

MyDialog constructor has an attribute parent.
With the code bellow, you create an instance of MyDialog with MyWidget as a parent:

self.dialogTextBrowser = MyDialog(self)

Two ways for a widget to access data from it's parent:

  • Use the parent attribute in the __init__ function

     self.lineEditData=parent.lineEdit.text()
    
  • Use the parent() method anywhere

     def run(self):
         self.lineEditData=self.parent().lineEdit.text()
    

I say it depends on how your suppose to use the application. If your suppose to fill the lineEdit once click and get a plot, I would use the parent attribute or directly pass the data in the __init__ function.
But if the user can go back to the lineEdit, change something, and click "run" again, then you should use the parent() method in run.

Mel
  • 5,837
  • 10
  • 37
  • 42
  • Thanks for your response. Using the parent() method in run works. Please clarify what you mean by "use the parent attribute or directly pass the data in the __init__ function" because I haven't been able to get this to work and and according to your advice these techniques are more suitable for my application. – Drew Oct 21 '15 at 20:14
  • Me I would init `MyDialog` with `self.dialogTextBrowser = MyDialog(self.lineEdit.text(),self)`, with a constructor `__init__(self, data, parent=None)`. This way the dialog is construct specifically for "data" and can't exist without it. But it is really just a personal preference, it just seems more logical to me. Using `parent()` is perfectly suitable too. – Mel Oct 22 '15 at 07:24