QQmlApplicationEngine 在设置后丢失上下文
QQmlApplicationEngine losing context after it set
下面的代码工作正确,我可以在 QML 文件中得到 my_model
属性。
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
context = engine.rootContext()
context.setContextProperty('my_model', MyModel())
engine.load('path/to/main.qml')
sys.exit(app.exec_())
但是当我尝试将 setContextProperty
移动到另一个函数时,QML 文件中的 my_model
为空。例如:
def boot_model(context):
context.setContextProperty('my_model', MyModel())
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
context = engine.rootContext()
boot_model(context)
engine.load('path/to/main.qml')
sys.exit(app.exec_())
在这种情况下,my_model
在我的 QML 文件中将为空:
ApplicationWindow {
id: root
Button {
onClicked: {
console.log(my_model) // Output: qml: null
}
}
}
我在尝试创建 QOject 工厂时发现了这个问题。
我做错了什么?
解释:
问题是 MyModel()
是一个局部变量,当 boot_model 函数执行完时,它会被删除。根据我的经验,我预计会出现两种行为,具体取决于 PyQt5 或 PySide2 的使用情况,在第一种情况下,我预计它会像您指示的那样指示 null,在第二种情况下,我预计会出现 Segmentation 错误(核心转储).
解决方案:
解决方案是扩展MyModel()
class对象的范围,为此有几种解决方案:
- 将父项传递给 MyModel:
def boot_model(context):
model = MyModel()
model.setParent(context)
context.setContextProperty("my_model", model)
- 使模型成为上下文的属性:
def boot_model(context):
model = MyModel()
context.model = model
context.setContextProperty("my_model", model)
下面的代码工作正确,我可以在 QML 文件中得到 my_model
属性。
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
context = engine.rootContext()
context.setContextProperty('my_model', MyModel())
engine.load('path/to/main.qml')
sys.exit(app.exec_())
但是当我尝试将 setContextProperty
移动到另一个函数时,QML 文件中的 my_model
为空。例如:
def boot_model(context):
context.setContextProperty('my_model', MyModel())
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
context = engine.rootContext()
boot_model(context)
engine.load('path/to/main.qml')
sys.exit(app.exec_())
在这种情况下,my_model
在我的 QML 文件中将为空:
ApplicationWindow {
id: root
Button {
onClicked: {
console.log(my_model) // Output: qml: null
}
}
}
我在尝试创建 QOject 工厂时发现了这个问题。
我做错了什么?
解释:
问题是 MyModel()
是一个局部变量,当 boot_model 函数执行完时,它会被删除。根据我的经验,我预计会出现两种行为,具体取决于 PyQt5 或 PySide2 的使用情况,在第一种情况下,我预计它会像您指示的那样指示 null,在第二种情况下,我预计会出现 Segmentation 错误(核心转储).
解决方案:
解决方案是扩展MyModel()
class对象的范围,为此有几种解决方案:
- 将父项传递给 MyModel:
def boot_model(context):
model = MyModel()
model.setParent(context)
context.setContextProperty("my_model", model)
- 使模型成为上下文的属性:
def boot_model(context):
model = MyModel()
context.model = model
context.setContextProperty("my_model", model)