graphWidget with PySide2

I followed the Embed PyQtgraph tutorial. I tried to write the code to use PySide2. But I don’t find the equivalent function to remplace graphWidget (PyQt5). Do you have any suggestion?

# from PyQt5 import QtWidgets, uic  # import for PyQt
from PySide2 import QtWidgets
from qtpy import uic
import sys  #  We need sys so that we can pass argv to QApplication

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        uic.loadUi('mainwindow.ui', self)  # Load the UI Page
        self.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [30, 32, 34, 32, 33, 31, 29, 32, 35, 45])

    def plot(self, hour, temperature):
        self.plot(hour, temperature)  # PySide2, work but plot out the main window
        # self.graphWidget.plot(hour, temperature)  # PyQt5

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Hi Adri,
graphWidget is not a function of PyQt5.
graphWidget is an instance of the class PlotWidget imported from the pyqtgraph.
Try to read the tutorial again.

@Luca the tutorial doesn’t have PySide2 code, so it doesn’t really help.

@Adri1G the main problem is you’re using Qt.py but not setting which Python Qt API to use and so it is defaulting to PyQt5. See the Requirements section here for possible values.

To set that in your code, you can just import os and then set the value on os.environ.
The following code works for me with PySide2.

import os

os.environ['QT_API'] = 'pyside2'

from PySide2 import QtWidgets

from qtpy import uic
import sys  #  We need sys so that we can pass argv to QApplication

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        uic.loadUi('mainwindow.ui', self)  # Load the UI Page
        self.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [30, 32, 34, 32, 33, 31, 29, 32, 35, 45])

    def plot(self, hour, temperature):
        self.graphWidget.plot(hour, temperature)  # PyQt5

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

That gives the same plot as for the PyQt5 version.

Ok. Thank you for your response :slight_smile:

1 Like