Python ReferenceError:“引用错误”;“什么?”;未在QML中定义
我有如下Main.qml文件:Python ReferenceError:“引用错误”;“什么?”;未在QML中定义,python,qt,qml,python-3.3,pyqt5,Python,Qt,Qml,Python 3.3,Pyqt5,我有如下Main.qml文件: import QtQuick 2.0 Rectangle { color: ggg.Colors.notificationMouseOverColor width: 1024 height: 768 } 在python文件中,我有以下内容(我使用PyQt5格式): 此python代码打印以下配置: {'Colors': {'chatInputBackgroundColor': '#AFAFAF', 'sideButtonSele
import QtQuick 2.0
Rectangle {
color: ggg.Colors.notificationMouseOverColor
width: 1024
height: 768
}
在python文件中,我有以下内容(我使用PyQt5格式):
此python代码打印以下配置:
{'Colors': {'chatInputBackgroundColor': '#AFAFAF', 'sideButtonSelectedColor': '#4872E8', 'sideButtonTextColor': '#787878', 'sideButtonSelectedTextColor': '#E2EBFC', 'sideButtonMouseOverColor': '#DDDDDD', 'buttonBorderColor': '#818181', 'notificationMouseOverColor': '#383838', }} <class 'dict'>
但是我不知道为什么会发生此错误,我如何修复此错误?在调用
View.setSource
之前,您需要设置上下文属性,否则在读取qml文件时,属性ggg
确实没有定义
试试这个:
App = QGuiApplication(sys.argv)
View = QQuickView()
Context = View.rootContext()
GlobalConfig = Config('sc').getGlobalConfig()
print (GlobalConfig, type(GlobalConfig))
Context.setContextProperty('ggg', GlobalConfig)
View.setSource(QUrl('views/sc_side/Main.qml'))
View.setResizeMode(QQuickView.SizeRootObjectToView)
View.showFullScreen()
sys.exit(App.exec_())
免责声明:在不知道什么是
Config
的情况下,我不能说它在没有任何其他修改的情况下是否真的可以工作。在加载QML文件之前,必须定义上下文属性,
它更好,因为它避免了警告和上下文重新加载
如果您在之后确实被迫这样做,只需在QML代码中添加一个安全性:
color: (typeof (ggg) !== "undefined" ? ggg.Colors.notificationMouseOverColor : "transparent");
然后,当您设置上下文属性时,它将重新加载上下文(不推荐),但至少不会发生错误。在
ggg.Colors.notificationMouseOverColor
?context.setContextProperty('ggg',GlobalConfig)
App = QGuiApplication(sys.argv)
View = QQuickView()
Context = View.rootContext()
GlobalConfig = Config('sc').getGlobalConfig()
print (GlobalConfig, type(GlobalConfig))
Context.setContextProperty('ggg', GlobalConfig)
View.setSource(QUrl('views/sc_side/Main.qml'))
View.setResizeMode(QQuickView.SizeRootObjectToView)
View.showFullScreen()
sys.exit(App.exec_())
color: (typeof (ggg) !== "undefined" ? ggg.Colors.notificationMouseOverColor : "transparent");