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

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

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

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!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak