各种 QML错误集合原因总汇

来源:互联网 发布:中国医疗人工智能大会 编辑:程序博客网 时间:2024/05/16 00:35
被这个错误坑了好久,原因记录下来:

第一点:
TypeError Cannot read property XXX of undefined
注意:检查与C++交互中有没有给初始化值,在界面起来之前,检查!检查!检查!
再这里还需要注意一点,复杂的数据结构的初始化,由于复杂的数据结构的初始化可能层层嵌套的值,在qml中没有提取到,
所以需要将该数据就够剖析出来,进行初始化赋值,这样做,虽然初始化麻烦了一点,但对于数据传输则更加便利,优化了数据的传输。

第二点:
ReferenceError: "something" is not defined in QML
注意:这是因为需要在调用context property之前需要调用view.setSource,否则会报此错
示例如下:
import QtQuick 2.0 Rectangle { color: ggg.Colors.notificationMouseOverColor width: 1024 height: 768}
in python file, i have this(i use form 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_())
this python code print this for config:
{'Colors': {'chatInputBackgroundColor': '#AFAFAF', 'sideButtonSelectedColor': '#4872E8', 'sideButtonTextColor': '#787878', 'sideButtonSelectedTextColor': '#E2EBFC', 'sideButtonMouseOverColor': '#DDDDDD', 'buttonBorderColor': '#818181', 'notificationMouseOverColor': '#383838', }} <class 'dict'>
when i run this code, my rectangle color change correctly, but i have this error:
file:///.../views/sc_side/Main.qml:6: ReferenceError: ggg is not defined


需要修改成这样:
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_())
参考文章:http://stackoverflow.com/questions/21430421/referenceerror-something-is-not-defined-in-qml


第三点
Unable to assign [undefined] to QString
注意:此次出现这个问题的原因如下,在qml中,我进行了一个封装,该封装的作用是对数据重复显示,不同数据调用同一个接收的property variant context,就出现了该错误,经过认真检查,该变量已经初始化,所以排除初始化的原因。
纠结了两个月,将该封装取出,使用最原始的方法进行显示,没有任何问题。
初步猜想,由于多个显示变量公用一个property接收,导致在接收其中一个变量时,其他变量报错,所以,该类型封装慎用。真正原因有待确定。
0 0
原创粉丝点击