Skip to content

Instantly share code, notes, and snippets.

@bgr
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgr/000aa9bb4593c532ca8a to your computer and use it in GitHub Desktop.
Save bgr/000aa9bb4593c532ca8a to your computer and use it in GitHub Desktop.
PyQt & QML object ownership problem
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import signal
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtCore import QUrl, QObject, QVariant, pyqtSlot
from PyQt5.QtQml import QQmlEngine, qmlRegisterType
class Dummy(QObject):
def __del__(self):
print("Deleted")
class GUIEntryPoint(QObject):
@pyqtSlot(result=QVariant)
def get_foo(self):
foo = Dummy()
print("Created {}".format(foo))
print("Ownership after instantiation: {}".format(QQmlEngine.objectOwnership(foo)))
#QQmlEngine.setObjectOwnership(foo, 1) # has no effect
return foo
@pyqtSlot(QVariant)
def print_foo(self, foo):
print("{}, ownership: {}".format(foo, QQmlEngine.objectOwnership(foo)))
def run():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QGuiApplication(sys.argv)
# these don't seem to make a difference
qmlRegisterType(GUIEntryPoint, 'GUIEntryPoint', 1, 0, 'GUIEntryPoint')
qmlRegisterType(Dummy, 'Dummy', 1, 0, 'Dummy')
qml_url = QUrl.fromLocalFile(os.path.join('zzz.qml'))
view = QQuickView()
gep = GUIEntryPoint()
view.rootContext().setContextProperty('BE', gep)
view.setSource(qml_url)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
import QtQuick 2.2
Rectangle {
id: rootItem
width: 640
height: 400
property var pleaseKeepMe: BE.get_foo()
Component.onCompleted: {
console.log("Completed");
BE.print_foo(pleaseKeepMe); // prints None, it has been deleted
console.log("creating another");
var x = BE.get_foo();
// at this point x has already been deleted on the Python side
console.log("created another");
BE.print_foo(x); // prints None
console.log("\n\nPress CTRL-C to exit");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment