PySide2 加载和创建组件时发生了什么?
What's happening while PySide2 load and create component?
此示例有效:
from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlComponent
from PySide2.QtQuick import QQuickView
app = QGuiApplication()
view = QQuickView()
engine = view.engine()
url = QUrl("main.qml")
view.setSource(url)
component = QQmlComponent(engine, QUrl.fromLocalFile("block.qml"))
item = component.create()
item.setParent(view.rootObject())
item.setParentItem(view.rootObject())
view.show()
app.exec_()
该项是 QQuickItem 的 class。
component.create() 是 QQuickItem 的 class。
将此示例更改为:
from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlComponent
from PySide2.QtQuick import QQuickView
app = QGuiApplication()
view = QQuickView()
engine = view.engine()
url = QUrl("main.qml")
view.setSource(url)
item = QQmlComponent(engine, QUrl.fromLocalFile("block.qml")).create()
item.setParent(view.rootObject())
item.setParentItem(view.rootObject())
view.show()
app.exec_()
抛出异常:
Traceback (most recent call last):
File "/home/babywolf/dev/apps/qttest/test.py", line 14, in <module>
item.setParent(view.rootObject())
RuntimeError: Internal C++ object (PySide2.QtQuick.QQuickItem) already deleted.
Process finished with exit code 1
怎么了?
main.qml:
import QtQuick 2.0
Rectangle {
width: 200
height: 200
color: "green"
}
block.qml:
import QtQuick 2.0
Text {
text: "Hello World"
anchors.centerIn: parent
}
这是 PySide 的一个特殊行为,如果一个变量没有分配给一个对象,它就会被消除,所以在这种情况下,QQmlComponent 被创建但没有分配给一个变量,所以它会被删除,这与第一种情况不同,因为它是分配给变量 component
,因此当想要创建项目时,组件不再存在,从而产生该错误。
一个解决方案是传递父级,因为在这种情况下可以查看延长其生命周期:
item = QQmlComponent(engine, QUrl.fromLocalFile("block.qml"), view).create()
# └----- parent
此示例有效:
from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlComponent
from PySide2.QtQuick import QQuickView
app = QGuiApplication()
view = QQuickView()
engine = view.engine()
url = QUrl("main.qml")
view.setSource(url)
component = QQmlComponent(engine, QUrl.fromLocalFile("block.qml"))
item = component.create()
item.setParent(view.rootObject())
item.setParentItem(view.rootObject())
view.show()
app.exec_()
该项是 QQuickItem 的 class。 component.create() 是 QQuickItem 的 class。 将此示例更改为:
from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlComponent
from PySide2.QtQuick import QQuickView
app = QGuiApplication()
view = QQuickView()
engine = view.engine()
url = QUrl("main.qml")
view.setSource(url)
item = QQmlComponent(engine, QUrl.fromLocalFile("block.qml")).create()
item.setParent(view.rootObject())
item.setParentItem(view.rootObject())
view.show()
app.exec_()
抛出异常:
Traceback (most recent call last):
File "/home/babywolf/dev/apps/qttest/test.py", line 14, in <module>
item.setParent(view.rootObject())
RuntimeError: Internal C++ object (PySide2.QtQuick.QQuickItem) already deleted.
Process finished with exit code 1
怎么了?
main.qml:
import QtQuick 2.0
Rectangle {
width: 200
height: 200
color: "green"
}
block.qml:
import QtQuick 2.0
Text {
text: "Hello World"
anchors.centerIn: parent
}
这是 PySide 的一个特殊行为,如果一个变量没有分配给一个对象,它就会被消除,所以在这种情况下,QQmlComponent 被创建但没有分配给一个变量,所以它会被删除,这与第一种情况不同,因为它是分配给变量 component
,因此当想要创建项目时,组件不再存在,从而产生该错误。
一个解决方案是传递父级,因为在这种情况下可以查看延长其生命周期:
item = QQmlComponent(engine, QUrl.fromLocalFile("block.qml"), view).create()
# └----- parent