Skip to content

Instantly share code, notes, and snippets.

@bgr
Created July 14, 2014 15:06
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/5072f0bb3d1c5623b6c1 to your computer and use it in GitHub Desktop.
Save bgr/5072f0bb3d1c5623b6c1 to your computer and use it in GitHub Desktop.
PyQt & QML ownership problem - reversed
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# this code is runnable in both Python 2 and 3
from __future__ import print_function
import os
import sys
import signal
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtCore import QUrl, QObject, pyqtProperty
from PyQt5.QtQml import qmlRegisterType
import sip
class Dummy(QObject):
counter = 0
def __init__(self, parent=None):
super(Dummy, self).__init__(parent)
self._number = Dummy.counter
Dummy.counter += 1
print("Instantiated", self._number)
# comment out this line and scroll the list to cause a segfault
sip.transferto(self, self)
@pyqtProperty(int, constant=True)
def number(self):
return self._number
def __del__(self):
print("Deleted", self._number)
def run():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QGuiApplication(sys.argv)
qmlRegisterType(Dummy, 'Dummy', 1, 0, 'Dummy')
qml_url = QUrl.fromLocalFile(os.path.join('zzz.qml'))
view = QQuickView()
view.setSource(qml_url)
def quitting():
print("\n\nquitting")
app.aboutToQuit.connect(quitting)
view.show()
return_code = app.exec_()
print("exited event loop")
sys.exit(return_code)
print("this never prints, as expected, however process remains running")
if __name__ == '__main__':
run()
import QtQuick 2.2
import Dummy 1.0
Item {
Text {
x: 50
text: "<-- scroll this list"
}
ListView {
width: 30
height: 80
spacing: 1
cacheBuffer: 0
model: 30
delegate: Rectangle {
width: 30
height: 14
color: "gray"
property var pleaseKeepMe: Dummy {}
Component.onDestruction: console.log("Killing", index)
Text {
text: pleaseKeepMe.number
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment