Intercepting the closeEvent without subclassing

56 views
Skip to first unread message

pytho...@gmail.com

unread,
Oct 26, 2016, 7:57:42 AM10/26/16
to pyqtgraph
Hi All,

I have a few questions regarding the closing of (real-time) plots using pyqtgraph with Python 2.7 and am hoping for some help.

1. I create two plots which update every second (Please see the code below). However, when I close one of the plots (by clicking on the x button in the top right corner of the window), the window closes but the corresponding timer is still keeps running.  I would like to intercept the event triggered when the x button is clicked and stop the timer in the event handler. Is there way to do this within my current code (i.e. without subclassing 'pg.PlotItem' and then overriding the closeEvent() method). I have also dabbled into event filters without much success.

2. Similar to above, what would be the best way to ensure that the plot releases memory when the user clicks the x button.

3. Lastly, and slightly off-topic, what is the best way to add custom text at certain locations on the plot window.

Many thanks for your help.

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import sys


class DataPlots(object):

   
def __init__(self, data, index, title, interval):
       
self.title = title
       
self.interval = interval
       
self.data = data
       
self.index = index
       
self.win = pg.GraphicsWindow()
       
self.win.setWindowTitle(self.title)
       
self.p = self.win.addPlot()
       
self.curve = self.p.plot(self.data[:self.index], pen='k')

       
self.timer = pg.QtCore.QTimer()
       
self.timer.timeout.connect(self.update)
       
self.timer.start(self.interval)

   
def update(self):
       
print 'Timer for: ' + self.title
       
self.index += 1
        self.curve.setData(self.data[:self.index])


if __name__ == '__main__':

    pg
.setConfigOption('background', 'w')
    pg
.setConfigOption('foreground', 'k')

    pg
.mkPen(1)

    x
= np.linspace(-np.pi, np.pi, 201)  # generate angle data from -pi to pi
    y1 = np.sin(x)  # generate an array of sin(angle)
    y2 = np.cos(x)  # generate an array of cose(angle)
    i = 2

    myPlot1 = DataPlots(y1, i, 'FirstPlot', 1000)
    myPlot2
= DataPlots(y2, i, 'SecondPlot', 1000)

   
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
       
QtGui.QApplication.instance().exec_()

Reply all
Reply to author
Forward
0 new messages