Created
June 4, 2015 14:24
-
-
Save rutsky/31f6cfb15c6aa474317a to your computer and use it in GitHub Desktop.
QQmlComponent.create() item ownership PyQt bug example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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