graphWidget with PySide2

Heads up! You've already completed this tutorial.

Adri1G | 2020-06-18 09:10:48 UTC | #1

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?

python
# 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().__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()
````

-------------------------

Luca | 2020-06-19 21:02:26 UTC | #2

Hi Adri,
graphWidget is not a function of PyQt5.
graphWidget is an instance of the class PlotWidget imported from the pyqtgraph.
-------------------------

martin | 2020-06-30 12:49:11 UTC | #3

@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](https://pypi.org/project/QtPy/).

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

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PySide2 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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

screenshot|627x500


Adri1G | 2020-06-30 12:49:40 UTC | #4

Ok. Thank you for your response :slight_smile:


Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

graphWidget with PySide2 was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.