Embedding pyqtgraph in PyQT

8,292 views
Skip to first unread message

Mike

unread,
Aug 30, 2012, 8:51:30 AM8/30/12
to pyqt...@googlegroups.com
Hi,

I am writing a program to analyse series of images (using the great ROI classes). I would like write a full application with a menu bar with menu items. I think that I need to embed pyqtgraph within pyqt4 to achieve this. Are there any examples of doing this? 

There is mention in the docs about using pyqt designer, but i thought there may be some code to do this. Most of the pyqtgraph examples have the following lines:

app = QtGui.QApplication([])
w   = pg.GraphicsWindow(size=(800,800), border=True)

but looking at the pyqt examples, i need to write a MainWindow class and a class to show an image. How would I go about this? Do I have the right approach?

Cheers,
Mike

Luke Campagnola

unread,
Aug 30, 2012, 9:15:57 AM8/30/12
to pyqt...@googlegroups.com
You're headed the right direction. All of the widgets in pyqtgraph are subclasses of QWidget, which means they can be added to a Qt GUI exactly as you would with any other QWidget. The window classes in pyqtgraph (GraphicsWindow, PlotWindow, etc.) are all examples of this--they are usually just a QMainWindow with a single widget embedded inside.

Here's a quick example:

app = QtGui.QApplication([])
win = QtGui.QMainWindow()
plot = pg.PlotWidget()
win.setCentralWidget(plot)

If you want to use Designer to build your GUI, you can include pyqtgraph widgets by adding placeholder widgets to the GUI, then using the "Promote To" feature to convert them (see the documentation for more information about this). Note that pyqtgraph widgets will not display correctly in Designer; they will only appear once the program is run.

Let me know if I can explain more about this.. I'm considering posting a video to explain the process a little better.

Luke
 

Mike

unread,
Aug 30, 2012, 9:33:39 AM8/30/12
to pyqt...@googlegroups.com


Hi Luke,

Yes, I just had a look at the class definition of GraphicsWindow. I can see how it works.

I don't particularly want to use Qt designer, it was just mentioned in the docs. At this stage my GUI is not that complication, I just want to add some menus, add an image and then some ROIs. 

Based on what you have said, does it sound feasible if I re-wrote the GraphicsView class (or created a sub class) that called my own version of QMainWindow. That is, I would just call it MainWindow and I would add menus to this class. 

Videos are always helpful - I wouldn't stop you doing that.

Thanks,
Mike

Luke Campagnola

unread,
Aug 30, 2012, 9:53:33 AM8/30/12
to pyqt...@googlegroups.com
On Thu, Aug 30, 2012 at 9:33 AM, Mike <michael.chri...@gmail.com> wrote:
Based on what you have said, does it sound feasible if I re-wrote the GraphicsView class (or created a sub class) that called my own version of QMainWindow. That is, I would just call it MainWindow and I would add menus to this class. 

I think you would be better off creating a subclass of QMainWindow that adds a GraphicsView as one of its child widgets. It isn't absolutely necessary to do it this way, but it's generally easier if the parent widgets manage the child widgets, rather than the opposite. Example:

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.view = pg.GraphicsView()
        self.setCentralWidget(self.view)    

Most PyQt examples you find will work this way as well.

Luke
 

Mike

unread,
Aug 31, 2012, 2:56:08 AM8/31/12
to pyqt...@googlegroups.com
Hi Luke,

I tried that and it works well. I also tried GraphicsLayoutWidget instead of GraphicsView which fits the image and provides zooming etc. I managed to create a main window with menu items, add an image and add some ROIs for the image analysis. 

Thanks for your help.

Cheers,
Mike


Mike

unread,
Aug 31, 2012, 8:29:56 AM8/31/12
to pyqt...@googlegroups.com

Hi,

I have another few questions before I get in too deep:

I am trying to analyse a series of images. I have a number of ROIs on each image i.e. I want to analyse how several regions change over time and then create a plot of this.

I was using graphicsView and creating several ROIs, but as I read more I realise that ImageView appears to have support for multiple image frames. Would ImageView be better suited to my application?

Also, ImageView currently appears to support a single ROI for each frame - is that correct? I need several, and will also need to save, delete and reload all my ROIs - this shouldn't be too bad, although it looks like I will need to keep track of the currently selected ROI so that I am able to save / delete the desired ROI. I see that there is a saveState for each ROI, but is there anything related to screen picking and then saving / deleting?

Thanks again for your help.

Cheers,
Mike

 


Luke Campagnola

unread,
Aug 31, 2012, 12:31:06 PM8/31/12
to pyqt...@googlegroups.com
On Fri, Aug 31, 2012 at 8:29 AM, Mike <michael.chri...@gmail.com> wrote:

I was using graphicsView and creating several ROIs, but as I read more I realise that ImageView appears to have support for multiple image frames. Would ImageView be better suited to my application?

That depends on the details of your application. It sounds like using ImageView is certainly a valid approach for you--it would take care of viewing frames over time as well as image contrast and coloring. 
 
Also, ImageView currently appears to support a single ROI for each frame - is that correct?

That is correct; you'll have to implement multiple ROIs. You could either implement your own system or try to extend ImageView's built-in ROI system to support multiple ROIs (probably the first option would be easier).
 
I need several, and will also need to save, delete and reload all my ROIs - this shouldn't be too bad, although it looks like I will need to keep track of the currently selected ROI so that I am able to save / delete the desired ROI. I see that there is a saveState for each ROI, but is there anything related to screen picking and then saving / deleting?

ROIs currently do not keep track of selection (although this is on my to-do list). To implement this, you should connect to ROI.sigClicked and keep track of which ROI was clicked last. It would probably make sense to change the color of the ROI while it is selected, too. 


Luke

Mike

unread,
Sep 2, 2012, 7:58:42 AM9/2/12
to pyqt...@googlegroups.com
Luke,

Thanks for your advice - I set up multiple ROIs and figured out how to keep track of the selected ROI as well as how to colour this ROI as well. This then allows me to save and load ROIs.

I can't figure out how I would go about deleting an ROI - would you have any idea?

Cheers,
Mike


Luke Campagnola

unread,
Sep 2, 2012, 9:25:52 AM9/2/12
to pyqt...@googlegroups.com
On Sun, Sep 2, 2012 at 7:58 AM, Mike <michael.chri...@gmail.com> wrote:
I can't figure out how I would go about deleting an ROI - would you have any idea?


The method you want is QGraphicsScene.removeItem(...)
So something like this: 

roi.scene().removeItem(roi)

Syed Zaim Nazir

unread,
Jan 15, 2013, 10:47:03 PM1/15/13
to pyqt...@googlegroups.com
Hi Luke,

I have similar question regarding embedding pyqtGraph. I have this application which is keeping track of the data and updating data on Pyqt-Window as well as PyqtGraph window which was created using :

self.win = pg.GraphicsWindow(title = self.name)
self.win.resize(800,600)

I create this separate pyqtGraph window using a button in the main application. The problem is I want to exit gracefully. Like when the main application exit, the pyqtGraph window should also close. I failed to do so. any advice??? 

Luke Campagnola

unread,
Jan 16, 2013, 9:26:41 AM1/16/13
to pyqt...@googlegroups.com
Sure, have a look at these:

If you call QApplication.quit(), this will force all windows to close. 
If you want to detect when the main window has closed, either override or intercept its close event. 


Luke


 

Syed Zaim Nazir

unread,
Jan 16, 2013, 6:07:39 PM1/16/13
to pyqt...@googlegroups.com
Hey thanks Loads. I was already overriding the closeEvent and QApplication.quit() worked for me. :D
 
 

Sun Jinjun

unread,
Jan 18, 2013, 4:24:08 AM1/18/13
to pyqt...@googlegroups.com
Why not use QtCreator create the layouts ?
After the design of layouts, go to finish all the remaining. At the same time use parameter tree class for the variables updating.


在 2012年8月30日星期四UTC+10下午10时51分30秒,Mike写道:

Zaim

unread,
Mar 27, 2013, 10:30:32 PM3/27/13
to pyqt...@googlegroups.com
This is question probably for luke,

I am having few trouble in embedding Pyqtgraph  in pyqt. I have a main pyqt window from where I launch my pyqtgraph window. If for some reason someone close my pyqtgraph window my application crashes for the reason that my program still tries to update graphs on pyqtgraph window. I failed to get any kinda of notifcation that pyqtgraph window is close now. I need a little direction where to look for 

I am using this to detect the closure of main window and overloading closeEvent : 
self.connect(self, SIGNAL('triggered()'), self.closeEvent)

How can I do the same for my pyqtgraph window??

self.tcxo_graphs = GraphFuntions2(length)

where GraphFunction class creates my graphs by using this:

self.win = pg.GraphicsWindow(title = self.name)       (import pyqtgraph as pg)
self.win.resize(800,600)

Luke Campagnola

unread,
Mar 28, 2013, 1:07:34 AM3/28/13
to pyqt...@googlegroups.com
On Wed, Mar 27, 2013 at 10:30 PM, Zaim <zaa...@gmail.com> wrote:
I am having few trouble in embedding Pyqtgraph  in pyqt. I have a main pyqt window from where I launch my pyqtgraph window. If for some reason someone close my pyqtgraph window my application crashes for the reason that my program still tries to update graphs on pyqtgraph window. I failed to get any kinda of notifcation that pyqtgraph window is close now. I need a little direction where to look for 

I am using this to detect the closure of main window and overloading closeEvent : 
self.connect(self, SIGNAL('triggered()'), self.closeEvent)

I'm not sure I understand where this 'triggered' signal is being emitted from.. 
 
How can I do the same for my pyqtgraph window??

You just need to handle the close event properly. You can do this either by making a subclass of GraphicsWindow and override closeEvent() or use an event filter to catch the event from somewhere else. 

Luke

 

Zaim

unread,
Mar 28, 2013, 2:03:32 AM3/28/13
to pyqt...@googlegroups.com
 "use an event filter to catch the event from somewhere else."  Can u explain this with some basic code ??

Luke Campagnola

unread,
Mar 28, 2013, 2:37:38 AM3/28/13
to pyqt...@googlegroups.com
On Thu, Mar 28, 2013 at 2:03 AM, Zaim <zaa...@gmail.com> wrote:
 "use an event filter to catch the event from somewhere else."  Can u explain this with some basic code ??

There's a simple example here (as well as many others floating around.. just search for qt event filter examples): 


Luke

Dirk Haupt

unread,
Mar 10, 2016, 7:16:56 PM3/10/16
to pyqtgraph
Hi, sorry to revive an almost half-decade old thread, but what you just described is exactly what I need. Do you have code for this example you go working 4 years ago? It would help likely help me immensely in getting started on a pyqt app of my own, thanx

I think I did find your end-product on github (not posted here as I figure that would compromise your anonymity) and though it works beautifully (with the exception that I have yet to figure out what your App's ROI text fields do) I was hoping you had the code for this simple example of a pyqt app with embedded pyqtGraph with which you can add ROI's?

Thanx,
Dirk

Mike

unread,
Mar 13, 2016, 7:33:19 PM3/13/16
to pyqtgraph
Hi Dirk,

Check out the simplified code at github.com/mhogg/ROIviewbox.

File "main.py" show you how to embed in a pyqt mainwindow. There are two other files, ROI.py and ViewBoxCustom.py, which are modified from pyqtgraph to support multiple ROIs. 

To draw a new ROI, just right click the mouse to bring up the menu.

I did this very quickly, but hopefully it will get you started. Currently there is no menu option to load an image, but this should be added easily enough.

Cheers,
Michael

Dirk Haupt

unread,
Mar 14, 2016, 2:27:34 PM3/14/16
to pyqtgraph
Thank you very much! This should help me greatly! 

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/jjUkmKaG030/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/18ddddda-4495-4018-8cde-902579991def%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sean Bellefeuille

unread,
Jul 20, 2016, 3:11:34 PM7/20/16
to pyqtgraph
Hi everyone,
I was wondering if someone could help me out. Similarly to the other posts I am trying to build graphs for my GUI application. I have been using designer and I used the graphics view. I converted my code to and I can now edit it but I know that I have to add my graph somewhere in here and don't know where to add it. I was wondering if anyone had any example or could walk me through how to add a graph to a GUI.

Thank you!

Sean

Kenneth Lyons

unread,
Jul 20, 2016, 9:12:34 PM7/20/16
to pyqtgraph
Since this seems to be a common issue and there doesn't seem to be a single document walking through the entire process, I put together a simple example and an explanation of the steps involved in embedding pyqtgraph plots in a PyQt UI.


If you need more details and/or better explanations of the steps involved, feel free to open an issue.

Sean Bellefeuille

unread,
Jul 22, 2016, 11:09:10 AM7/22/16
to pyqtgraph
Thank you so much! Great explanation
Reply all
Reply to author
Forward
0 new messages