1

I tried to update my code from python 2.7 to 3.7.4. Additonally, the matplotlib backend changed from qt4 to qt5. In the code, I check using sip.isdeleted() if the canvas of the matplotlib is deleted or not as a workaround for the wrapped C/C++ object of type QAction has been deleted error.

In python 2.7 and using qt4, this did work.

After updating the code, an error is thrown in sip.isdeleted():

Exception occurred in traits notification handler for object: <lasers.profiles.AnsysSag object at 0x000001BA34014828>, trait: raw_series, old value: None, new value: <lasers.series.Series object at 0x000001BA30789D68>
Traceback (most recent call last):
  File "C:\Users\pujoel\AppData\Local\Continuum\anaconda3\lib\site-packages\traits\trait_notifiers.py", line 591, in _dispatch_change_event
    self.dispatch(handler, *args)
  File "C:\Users\pujoel\AppData\Local\Continuum\anaconda3\lib\site-packages\traits\trait_notifiers.py", line 553, in dispatch
    handler(*args)
  File "C:\Users\pujoel\Documents\LaserS\src\python\lasers\profiles.py", line 151, in plot
    if canvas is not None and not sip.isdeleted(canvas):
TypeError: isdeleted() argument 1 must be sip.simplewrapper, not FigureCanvasBase

The code section where this error is triggered is shown below. Raw_series and series are traits classes with two arrays data and base as properties.

from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import sip


class Profile(ABCHasTraits):
    raw_series = Instance(HasTraits)
    series = Property()

    _figure = Instance(Figure)
    figure = Property(depends_on='_figure')

    def __init__(self, parent=None):
        ABCHasTraits.__init__(self)
        self._init_figure()

    def _init_figure(self):
        self._figure = Figure()
        ax = self.figure.add_subplot(111)
        ax.grid(which='both')

    def plot(self):
        series = self.series
        if series:
            axes = self.figure.axes
            for ax in axes:
                ax.clear()
                ax.grid(which='both')

            axes[0].plot(series.base, series.data) 

            canvas = self.figure.canvas
             #if viewer is not the active tab in the tree, the QT object is deleted, leaving an empty wrapper. Calling draw() then gives an error
            if canvas is not None and not sip.isdeleted(canvas):
                canvas.draw()

    def traits_view(self):
        return View(Item('name'),
                    Group(
                        UItem('figure', editor=MPLFigureEditor()),
                        show_border=True, visible_when='detrend_data==False'),
                    resizable=True,
                    width=.5,
                    id='lasers.profiles')

The matplotlib figure editor used in the view is defined like this:

matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT
from matplotlib.figure import Figure

from traits.api import Instance
from traitsui.qt4.editor import Editor
from traitsui.qt4.basic_editor_factory import BasicEditorFactory

class _MPLFigureEditor(Editor):

    scrollable  = True

    def init(self, parent):
        self.control = self._create_canvas(parent)
        self.set_tooltip()

    def update_editor(self):
        pass

    def _create_canvas(self, parent):
        """ Create the MPL canvas. """
        # matplotlib commands to create a canvas

        frame = QtGui.QWidget()
        mpl_canvas = FigureCanvas(self.value)
        mpl_toolbar = NavigationToolbar2QT(mpl_canvas, frame)

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(mpl_toolbar)
        vbox.addWidget(mpl_canvas)
        frame.setLayout(vbox)

class MPLFigureEditor(BasicEditorFactory):

    klass = _MPLFigureEditor

So far I tried to update sip to PyQt5.sip as well as omitting sip completeley. Both actions did not solve the error. Omitting sip completely results in the wrapped C/C++ object of type QAction has been deleted error (PyQt: RuntimeError: wrapped C/C++ object has been deleted).

So far, I did not find any solution to this problem and I would really appreciate your help.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35

0 Answers0