Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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)
import QtQuick 2.0
Rectangle {
Component.onCompleted: {
Component.onDestruction: {
"""), QUrl())
item = component.create()
assert item.parent() is None
# According to sip.dump(), item is owned by C++, but should be owned by
# Python
# <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.
del item
# The QML item should be destroyed at this point, but it's not.
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment