Issue
QtWebkit
segfaults whenever I return a QObject
from a method of another QObject
.
Here's the simplest test case that I can come up with (it works for both PySide and PyQt4):
#!/usr/bin/env python2
import sys
try:
from PyQt4.QtCore import QObject, QVariant, pyqtProperty
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView
Property = pyqtProperty
except ImportError:
from PySide.QtCore import QObject, Property
from PySide.QtGui import QApplication
from PySide.QtWebKit import QWebView
class TestObject(QObject):
@Property(str)
def property(self):
return 'foo'
class TestObjectContainer(QObject):
@Property(QObject) # Swapping QObject with TestObject doesn't fix it
def object(self):
return TestObject()
if __name__ == '__main__':
application = QApplication(sys.argv)
browser = QWebView()
frame = browser.page().mainFrame()
frame.addToJavaScriptWindowObject('container', TestObjectContainer())
frame.addToJavaScriptWindowObject('test_object', TestObject())
browser.show()
#frame.evaluateJavaScript('test_object.property')
frame.evaluateJavaScript('container.object.property')
sys.exit(application.exec_())
Here is the odd part:
test_object.property; // This doesn't segfault
container.object.property; // This does
Is there any way to return QObject
s like this? Am I doing something wrong, or is this a bug?
I ended up creating a simple wrapper class with the help of @HYRY's answer:
class WebkitQObject(QObject):
def __init__(self):
super(WebkitQObject, self).__init__()
self.__cache__ = []
def store(self, item):
self.__cache__.append(item)
return self.__cache__[-1]
Now, this code works with a slight modification:
class TestObjectContainer(WebkitQObject):
@Property(QObject)
def object(self):
return self.store(TestObject())
Solution
You need a Python reference to the QObject to keep it alive. You can test this by changing the code as following,
class TestObjectContainer(QObject):
@Property(QObject)
def object(self):
self.tmp = TestObject()
return self.tmp
Answered By - HYRY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.