string QML Value Type

A free form text string. 更多...

详细描述

The string type refers to a free form text string in quotes, for example "Hello world!". The QML language provides this value type by default.

范例:

Text { text: "Hello world!" }
					

Properties of type string are empty by default.

Strings have a length attribute that holds the number of characters in the string.

The string value type is backed by the C++ type QString . It extends the JavaScript String primitive type in that it provides much of the same API, plus some extra methods. For example, the QML string value type method arg() supports value substitution:

var message = "There are %1 items"
var count = 20
console.log(message.arg(count))
					

The example above prints "There are 20 items".

The QML string value type supports most of the ECMAScript string features, such as template (string) literals, string interpolation, multi-line strings, and looping over strings.

In general, QML string supports most JavaScript String methods, including checking for inclusion using string.includes() , string.startsWith() ,和 string.endsWith() ; repeating a string using string.repeats() , and slicing and splitting using string.slice() and string.split() .

For more information about which version of ECMAScript QML supports, see JavaScript 主机环境

For more information about JavaScript String methods, see mdn JavaScript String

When integrating with C++, note that any QString passed into QML from C++ is automatically converted into a string value, and vice-versa.

另请参阅 QML 值类型 and ECMAScript 语言规范 .