string QML Value Type
自由格式文本字符串。更多
详细说明
string
类型指带引号的自由格式文本字符串,例如 "Hello world!"。QML 语言默认提供这种值类型。
举例说明:
Text { text: "Hello world!" }
string
类型的属性默认为空。
字符串有一个length
属性,用于保存字符串中的字符数。
字符串值类型由 C++ 类型QString 支持。它扩展了 JavaScript 字符串原始类型,提供了大部分相同的 API 和一些额外的方法。例如,QML 字符串值类型方法arg()
支持值替换:
var message = "There are %1 items" var count = 20 console.log(message.arg(count))
上面的例子打印了 "有 20 个项目"。
QML string 值类型支持大多数 ECMAScript string 功能,如模板(字符串)字面量、字符串插值、多行字符串和字符串循环。
一般来说,QML string 支持大多数 JavaScript String 方法,包括使用string.includes()
,string.startsWith()
, 和string.endsWith()
检查是否包含字符串;使用string.repeats()
重复字符串;使用string.slice()
和string.split()
分割字符串。
有关 QML 支持的 ECMAScript 版本的更多信息,请参阅JavaScript Host Environment。
有关 JavaScript 字符串方法的更多信息,请参阅mdn JavaScript String。
与 C++ 整合时,请注意从 C++ 传入 QML 的任何QString 值都会自动转换为string
值,反之亦然。
另请参阅 QML 值类型和ECMAScript 语言规范。
© 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.