[PySide] Internal C++ object (PySide.QtWebKit.QWebFrame) already deleted; but I'm saving it as an attribute to avoid this exact issue

Jorge Araya Navarro elcorreo at deshackra.com
Thu Nov 6 23:07:05 CET 2014

Stefan Champailler writes:

> Hi Jorge,
> Would you care to send a shorter version of the code (with the bug, that is) ? It'd help us to reproduce it (and it'd help you to better understand it)
> Stefan

Sure, for a minimal example that you can run, you will require to download the file attached. 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: webelementinfo.py
Type: text/x-python
Size: 3730 bytes
Desc: code that defines WebElement class
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20141106/f998f7bc/attachment.py>
-------------- next part --------------

Here is the code that displays the issue, after starting it, just move the mouse over the web site, here is
the code:

--8<---------------cut here---------------start------------->8---
#!/usr/bin/env python2
# coding: utf-8
#                        VENI, SANCTE SPIRITUS

from PySide.QtWebKit import QWebView
from PySide import QtCore, QtGui
    from . import webelementinfo
except ValueError:
    import webelementinfo

class CustomQWebView(QWebView):

    def __init__(self, *args, **kwargs):
        """ Init the custom class
        super(CustomQWebView, self).__init__(*args, **kwargs)
        self.colors = {0: QtGui.QColor(255, 165, 0, 127),
                       1: QtGui.QColor(135, 206, 235, 127),
                       2: QtGui.QColor(135, 235, 164, 127),
                       3: QtGui.QColor(235, 135, 206, 127),
                       4: QtGui.QColor(235, 164, 135, 127)}
        self.color = None
        self.currentframe = None
        self.element = None

    def selectCommentsArea(self):
        """ For selecting the comment area

    def mouseMoveEvent(self, event):
        super(CustomQWebView, self).mouseMoveEvent(event)

        if self.drawrects:
            if self.currentframe:
                hittestresult = self.currentframe.hitTestContent(event.pos())
                element = webelementinfo.WebElement(
                    hittestresult, self.color, self)
                if not self.element:
                    self.element = element
                elif self.element != element:
                    self.element = element

                # FIXME: self.update should draw rects from WebElements too.

    def paintEvent(self, event):
        # draw the content first
        super(CustomQWebView, self).paintEvent(event)

        if self.drawrects:
            # then the rectangle
            if self.element:

    def setframeafterloadfinished(self):
        self.currentframe = self.page().mainFrame()

    def setup_rectcolor_area(self, forarea):
        """Called when we want to select certain area of a web site

        This method set-up the painter to a giving color so web elements are
        drawn with a rect on top. Also activates the flag to allow painting
        inside CustomQWebView.

        :param int forarea: For which area we are going to set the painter\\
        valid values are: 0 for Comments area, 1 for comment box, 2 for\\
        commentator's user name, 3 for comment date and time, 4 for\\
        commentary text.
        self.drawrects = True
        self.color = self.colors[forarea]

        # defines what we are looking to select
        self.selecttype = forarea

if __name__ == "__main__":
    app = QtGui.QApplication([])
    mainwn = QtGui.QMainWindow()
    mainwn.resize(800, 696)
    centralwidget = QtGui.QWidget(mainwn)
    centralwidget.resize(800, 600)
    gridlayout = QtGui.QGridLayout(centralwidget)
    web = CustomQWebView(parent=centralwidget)  # I do this too in my main interface code
    gridlayout.addWidget(web, 0, 0, 1)          # but wasn't a problem before adding my class WebElement

--8<---------------cut here---------------end--------------->8---

Pax et bonum.
Jorge Araya Navarro.
ES: DiseƱador Publicitario, Programador Python y colaborador en Parabola GNU/Linux-libre
EN: Ads Designer, Python programmer and contributor Parabola GNU/Linux-libre
EO: Anonco grafikisto, Pitino programalingvo programisto kai kontribuanto en Parabola GNU/Linux-libre

More information about the PySide mailing list