ReferenceError: “something” is not defined in QML

左心房为你撑大大i 提交于 2019-12-01 03:52:14

You need to set the context property before you call View.setSource, otherwise at the moment the qml file is read the property ggg is indeed not defined.

Try this:

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_())

Disclaimer: without knowing what Config is, I can't say if it will really work without any other modification.

You have to define the context property before loading QML file, it's better because it avoids warnings and context reloading.

If you are REALLY forced to do it after, just add a security in your QML code :

color: (typeof (ggg) !== "undefined" ? ggg.Colors.notificationMouseOverColor : "transparent");

Then when you will set the context property it will reload the context (not recommanded) but at least no error will happen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!