Skip to content

Instantly share code, notes, and snippets.

Embed
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)
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
You can’t perform that action at this time.