python – ReferenceError:QML中没有定义“something”

我有这样的Main.qml文件
import QtQuick 2.0    

Rectangle {
    color: ggg.Colors.notificationMouSEOverColor
    width: 1024
    height: 768
}

在python文件中,我有这个(我使用形式PyQt5):

App = QGuiApplication(sys.argv)
View = qquickview()
View.setSource(QUrl('views/sc_side/Main.qml'))
Context = View.rootContext()

GlobalConfig = Config('sc').getGlobalConfig()
print (GlobalConfig,type(GlobalConfig))
Context.setContextProperty('ggg',GlobalConfig)

View.setResizeMode(qquickview.SizeRootObjectToView)
View.showFullScreen()
sys.exit(App.exec_())

这个python代码打印这个配置:

{'Colors': {'chatInputBackgroundColor': '#AFAFAF','sideButtonSelectedColor': '#4872E8','sideButtonTextColor': '#787878','sideButtonSelectedTextColor': '#E2EBFC','sideButtonMouSEOverColor': '#ddddDD','buttonBorderColor': '#818181','notificationMouSEOverColor': '#383838',}} <class 'dict'>

当我运行此代码时,我的矩形颜色正确更改,但我有这个错误

file:///.../views/sc_side/Main.qml:6: ReferenceError: ggg is not defined

但我不知道为什么会发生这个错误,我怎么能解决这个错误

解决方法

您需要在调用View.setSource之前设置context属性,否则在读取qml文件时,确实没有定义属性ggg.

试试这个:

App = QGuiApplication(sys.argv)
View = qquickview()
Context = View.rootContext()

GlobalConfig = Config('sc').getGlobalConfig()
print (GlobalConfig,GlobalConfig)

View.setSource(QUrl('views/sc_side/Main.qml'))    
View.setResizeMode(qquickview.SizeRootObjectToView)
View.showFullScreen()
sys.exit(App.exec_())

免责声明:在不知道Config是什么的情况下,我不能说它是否会在没有任何其他修改的情况下真正起作用.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...