plotting through ipython and QT4 custom widgets

Hi,

I want to add a QT4 widget to a matplotlib figure, but the widget does not react to user input.

Here it is a test case:

from PyQt4 import QtGui, QtCore
from pylab import *

def test():

plot([1,2,3], lw=2)
qt4_interface(gcf())

class qt4_interface:
def init(self,fig):
QMainWin = fig.canvas.parent()
toolbar = QtGui.QToolBar(QMainWin)
QMainWin.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar)

    self.line_edit = QtGui.QLineEdit(parent=toolbar)
    self.line_edit.editingFinished.connect(self.do_something)
    toolbar.addWidget(self.line_edit)

def do_something(self, *args):

    f = open('l','a'); f.write('yes\n'); f.flush(); f.close()
    #close()

I run the script as “run -i qt4_test.py” from ipython. Then running test() I get the figure with the additional widget but the do_something method is never called.

Incidentally if I do a plot from ipython and then I type interactively qt4_interface(gcf()), the qt4 widget is added to the figure and works properly.

Any hints on how can I resolve this problem?

BTW, I’m running matplotlib official package (1.0.1) included in ubuntu 11.10.

Thanks,
Antonio

qt4_test.py (609 Bytes)

Hi,

same problem with ipython 0.12 and matplotlib 1.1.1rc.

To recall, I’m trying to add a QT4 widget to a matplotlib figure (MPL is using Qt4 as backend). However, in the attached example the widget callback (or slot) is not called. Oddly, if I add the qt4 widget manually calling qt4_interface() from ipython it works.

Basically I want to preserve the maplotlib+ipython interactive workflow, but using some “enhanced” figures (i.e. mpl figure+qt4 widgets).

Thank you for any suggestion.
Antonio

2012/5/24 AI <tritemio@…287…>

···

Hi,

I want to add a QT4 widget to a matplotlib figure, but the widget does not react to user input.

Here it is a test case:

from PyQt4 import QtGui, QtCore
from pylab import *

def test():

plot([1,2,3], lw=2)
qt4_interface(gcf())

class qt4_interface:
def init(self,fig):
QMainWin = fig.canvas.parent()
toolbar = QtGui.QToolBar(QMainWin)
QMainWin.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar)

    self.line_edit = QtGui.QLineEdit(parent=toolbar)
    self.line_edit.editingFinished.connect(self.do_something)
    toolbar.addWidget(self.line_edit)

def do_something(self, *args):




    f = open('l','a'); f.write('yes\n'); f.flush(); f.close()
    #close()

I run the script as “run -i qt4_test.py” from ipython. Then running test() I get the figure with the additional widget but the do_something method is never called.

Incidentally if I do a plot from ipython and then I type interactively qt4_interface(gcf()), the qt4 widget is added to the figure and works properly.

Any hints on how can I resolve this problem?

BTW, I’m running matplotlib official package (1.0.1) included in ubuntu 11.10.

Thanks,
Antonio

Hi to all,

I found the solution. I report it for reference.

When you add a QT4 widget to a matplotlib figure always save a reference of the QT4 widget, otherwise the object will be deleted (even if it is shown in the GUI) and the user interaction will not work.

Here it it the working example (also attached). Run it from ipython with “run -i …” command.

from PyQt4 import QtGui, QtCore

def test():
plot([1,2,3], lw=2)
q = qt4_interface(gcf())

return q   # WARNING: it's paramount to return the object otherwise, with
           # no references, python deletes it and the GUI doesn't respond!

class qt4_interface:
def init(self,fig):

    self.fig = fig

    QMainWin = fig.canvas.parent()
    toolbar = QtGui.QToolBar(QMainWin)
    QMainWin.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar)

    self.line_edit = QtGui.QLineEdit()#parent=toolbar)

    toolbar.addWidget(self.line_edit)
    self.line_edit.editingFinished.connect(self.do_something)

def do_something(self, *args):
    self.fig.axes[0].set_title(self.line_edit.text())
    self.fig.canvas.draw()

    #f = open('l','a'); f.write('yes\n'); f.flush(); f.close()

Regards,
Antonio

matplotlib_figure+qt4_widget.py (849 Bytes)

···

On Fri, May 25, 2012 at 9:50 AM, AI <tritemio@…1003…7…> wrote:

Hi,

same problem with ipython 0.12 and matplotlib 1.1.1rc.

To recall, I’m trying to add a QT4 widget to a matplotlib figure (MPL is using Qt4 as backend). However, in the attached example the widget callback (or slot) is not called. Oddly, if I add the qt4 widget manually calling qt4_interface() from ipython it works.

Basically I want to preserve the maplotlib+ipython interactive workflow, but using some “enhanced” figures (i.e. mpl figure+qt4 widgets).

Thank you for any suggestion.
Antonio

2012/5/24 AI <tritemio@…287…>

Hi,

I want to add a QT4 widget to a matplotlib figure, but the widget does not react to user input.

Here it is a test case:

from PyQt4 import QtGui, QtCore
from pylab import *

def test():

plot([1,2,3], lw=2)
qt4_interface(gcf())

class qt4_interface:
def init(self,fig):
QMainWin = fig.canvas.parent()
toolbar = QtGui.QToolBar(QMainWin)
QMainWin.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar)

    self.line_edit = QtGui.QLineEdit(parent=toolbar)
    self.line_edit.editingFinished.connect(self.do_something)
    toolbar.addWidget(self.line_edit)

def do_something(self, *args):





    f = open('l','a'); f.write('yes\n'); f.flush(); f.close()
    #close()

I run the script as “run -i qt4_test.py” from ipython. Then running test() I get the figure with the additional widget but the do_something method is never called.

Incidentally if I do a plot from ipython and then I type interactively qt4_interface(gcf()), the qt4 widget is added to the figure and works properly.

Any hints on how can I resolve this problem?

BTW, I’m running matplotlib official package (1.0.1) included in ubuntu 11.10.

Thanks,
Antonio