Skip to content

Instantly share code, notes, and snippets.

@rutsky
Created June 4, 2015 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rutsky/31f6cfb15c6aa474317a to your computer and use it in GitHub Desktop.
Save rutsky/31f6cfb15c6aa474317a to your computer and use it in GitHub Desktop.
QQmlComponent.create() item ownership PyQt bug example
import sys
import textwrap
import sip
from PyQt5.QtQml import QQmlEngine, QQmlComponent
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
def main():
app = QApplication(sys.argv)
qml_engine = QQmlEngine()
component = QQmlComponent(qml_engine)
component.setData(textwrap.dedent("""\
import QtQuick 2.0
Rectangle {
Component.onCompleted: {
console.log("created");
}
Component.onDestruction: {
console.log("destroyed");
}
}
"""), QUrl())
item = component.create()
assert item.parent() is None
# According to sip.dump(), item is owned by C++, but should be owned by
# Python
#sip.dump(item)
# <PyQt5.QtCore.QObject object at 0x7f411e1c69d8>
# Reference count: 2
# Address of wrapped object: 0x22c0fb0
# Created by: C/C++
# To be destroyed by: C/C++
# Parent wrapper: NULL
# Next sibling wrapper: NULL
# Previous sibling wrapper: NULL
# First child wrapper: NULL
# Explicit transfer of the ownership to Python fixes issue with not
# destroyed QML item.
#sip.transferback(item)
del item
# The QML item should be destroyed at this point, but it's not.
input()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment