The types which may be used in the definition of an object hierarchy in a QML document can come from various sources. They may be:
Furthermore, application developers can provide their own types, either by registering C++ types directly, or by defining reusable components in QML documents which can then be imported.
Wherever the type definitions come from, the engine will enforce type-safety for properties and instances of those types.
The QML language has built-in support for various primitive types including integers, double-precision floating point numbers, strings, and boolean values. Objects may have properties of these types, and values of these types may be passed as arguments to methods of objects.
见 QML 基本类型 文档编制了解基本类型的更多有关信息。
JavaScript objects and arrays are supported by the QML engine. Any standard JavaScript type can be created and stored using the generic var 类型。
例如,标准
Date
and
数组
类型是可用的,如下:
import QtQuick 2.0 Item { property var theArray: [] property var theDate: new Date() Component.onCompleted: { for (var i = 0; i < 10; i++) theArray.push("Item " + i) console.log("There are", theArray.length, "items in the array") console.log("The time is", theDate.toUTCString()) } }
见 在 QML 文档中的 JavaScript 表达式 了解更多细节。
A QML object type is a type from which a QML object can be instantiated. QML object types are derived from
QtObject
, and are provided by QML modules. Applications can import these modules to use the object types they provide. The
QtQuick
module provides the most common object types needed to create user interfaces in QML.
Finally, every QML document implicitly defines a QML object type, which can be re-used in other QML documents. See the documentation about 在 QML 类型系统中的对象类型 for in-depth information about object types.