组件缺少一个必填属性
此警告类别由 qmllint 拼写为[required]
。
组件缺少一个必填属性
发生了什么事?
组件的必填属性未设置。
为什么会这样?
QML 应用程序中的组件如果缺少必填属性,就会行为不端:如果静态检测到缺少必填属性,就根本无法启动。动态创建的组件如果缺少必填属性,运行时将无法创建:它们将被置为空。
示例
import QtQuick Item { component RepeatMe: Item { required property int index; required property int helloWorld; } RepeatMe {} // not ok: required properties index and helloWorld not set Repeater { model: 10 RepeatMe {} // not ok: required property index set by Repeater, but not helloWorld } }
要修复此警告,请设置必填属性:
import QtQuick Item { component RepeatMe: Item { required property int index; required property int helloWorld; } RepeatMe { index: 0 helloWorld: 42 } // ok: all required properties were set Repeater { model: 10 RepeatMe { helloWorld: index * 2 + 1 } // ok: all required properties were set: index by the Repeater and helloWorld by the user } }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.