加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 23:15:01 所属栏目:Python 来源:网络整理
导读:我有这样的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(
我有这样的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是什么的情况下,我不能说它是否会在没有任何其他修改的情况下真正起作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读