Embedding a pyqtchart into the Graphics View in qt designer

7,273 views
Skip to first unread message

Su-Shin Ang

unread,
Dec 16, 2013, 7:26:26 AM12/16/13
to pyqt...@googlegroups.com
Hi all,

I am trying to embed a pyqtgraph, i.e a PlotWidget, into a GraphicsView window created within qt designer. I followed the steps included in the documentation:

"Pyqtgraph’s widgets can be included in Designer’s ui files via the “Promote To...” functionality:

  1. In Designer, create a QGraphicsView widget (“Graphics View” under the “Display Widgets” category).
  2. Right-click on the QGraphicsView and select “Promote To...”.
  3. Under “Promoted class name”, enter the class name you wish to use (“PlotWidget”, “GraphicsLayoutWidget”, etc).
  4. Under “Header file”, enter “pyqtgraph”.
  5. Click “Add”, then click “Promote”."
After this, how do I use the methods within the widget? I saw some suggestions about doing this in the Main Window class? Is that the right place to do so? If possible, I would really appreciate a code stub, providing an example of how the widget might be deployed.

Thanks in advance!
Ed 


Luke Campagnola

unread,
Dec 17, 2013, 9:36:06 PM12/17/13
to pyqt...@googlegroups.com
There are a few different approaches to getting your .ui file loaded in to python. See: http://stackoverflow.com/questions/2398800/linking-a-qtdesigner-ui-file-to-python-pyqt

The VideoSpeedTest and ScatterPlotSpeedTest examples in pyqtgraph both use .ui files that were compiled using the tools/rebuildUi.py script (which just calls pyuic4 or pyside-uic, depending on which you have). You could look through those to see how they access the widgets defined in the .ui file.

I have just added an example of another approach to the repository at github: https://github.com/pyqtgraph/pyqtgraph/commit/4e9e75817fb0d2b5f6880278857621af30d7d0f1
These directly load the .ui file into python at runtime, rather than compiling python modules with uic.

Let us know if you need more help with this..


Luke

Fabian Salinas

unread,
Aug 24, 2017, 7:26:27 PM8/24/17
to pyqtgraph
Hi!

were you able to embed your pyqtgraph? I'm trying the same with a plot I created using pyqtgraph

Alejandro Condori

unread,
Oct 22, 2017, 4:19:49 PM10/22/17
to pyqtgraph
I use Spyder (from Anaconda)(Python 3.6 and PyQt5). Try to run something like this:

from PyQt5 import uic,QtWidgets,QtGui
import numpy as np

form_class = uic.loadUiType("yourUIfile.ui")[0]

class MyWindowClass(QtWidgets.QMainWindow,form_class):
     def __init__(self, parent=None):
          QtWidgets.QMainWindow.__init__(self, parent)
          self.setupUi(self)
         # here you can properties of your graph widget.... for example:
          self.GraphWidgetName.setTitle('My Graph')
          self.GraphWidgetName.setLabel('bottom', 'X axis')
          self.GraphWidgetName.setLabel('left', 'Y axis')
          plotexample()
     

      def plotexample(self):
          a=np.array([1,2,3,4,5])
          b=np.array([6,7,8,9,10])
          self.GraphWidgetName.plot(a,b)
          self.GraphWidgetName.showGrid(x=True, y=True)

if __name__=="__main__":
    app = QtWidgets.QApplication(sys.argv)
    MyWindow = MyWindowClass(None)
    MyWindow.show()
    app.exec_()

I made this right now, please try it and tell me if this works for you, dont forget to change the name like "yourUIfile.ui" for the names of the files you have. In my case i didn't have to import pyqtgraph library because the widget already have the properties of pyqtgraph (this only if you promoted QGraphicsView Widget correctly). Well if this dont works try adding "import pyqtgraph" at the start of the script. Good Luck...

 Alejandro
PD: English is not my mother language, i hope you will understand what i wrote.

Hans-Jakob Zuglert

unread,
Dec 11, 2017, 4:08:28 AM12/11/17
to pyqtgraph
Dear Alejandro,

Many thanks! I use almost the same setup (Spyder 3.2.4, Python 3.5.2 64bits, PyQt5.6 on Windows)

Small thing: "import sys" is missing, I guess.

I tried your code and it seems clear to me, except one point, but I probably miss a bit of background:
you define "plotexample" as a method in the "MyWindowClass". And run it in the constructor (well _init__).
When I try to execute then Spyder prompts "undifined name 'plotexample' ". so I took it out and I am not sure where to call it from here...
Everything else works nicely.

I had to adapt the execution part, otherwise every second run of the program kills the kernel:

from PyQt5 import uic,QtWidgets,QtGui
import pyqtgraph
import numpy as np
import sys

form_class = uic.loadUiType("Test_Average-GUI.ui")[0]


class MyWindowClass(QtWidgets.QMainWindow,form_class):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        # here you can properties of your graph widget.... for example:
        self.GraphWidgetName.setTitle('My Graph')
        self.GraphWidgetName.setLabel('bottom', 'X axis')
        self.GraphWidgetName.setLabel('left', 'Y axis')
       
    def plotexample(self):
        a=np.array([1,2,3,4,5])
        b=np.array([6,7,8,9,10])
        self.GraphWidgetName.plot(a,b)
        self.GraphWidgetName.showGrid(x=True, y=True)

if __name__=="__main__":
    app = QtWidgets.QApplication.instance() #Check for an instance of a QtWidgets.QApplication, if so use it...
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

Jan Sebastian

unread,
May 24, 2018, 6:13:42 AM5/24/18
to pyqtgraph
is there any other ways to promote more than one class? So actualy I need to use PlotWidget and GhrapicLayoutWidget
Reply all
Reply to author
Forward
0 new messages