qml 基本数据类型

Qt QML支持多种基本类型,包括int、string等,它们不同于具有属性、信号和方法的对象类型。基本类型用于表示简单值,当赋值给另一个变量或属性时,会复制值。QML内置了一些基本类型,并允许通过导入模块扩展更多类型。基本类型的属性变更不会发出自己的改变信号,而对象类型的属性则会单独发出改变信号。
摘要由CSDN通过智能技术生成
都是
  • Qt 5.15
  • Qt QML
  • QML Basic Types
  • Qt 5.15.1 Reference Documentation

Contents

QML Basic Types

QML supports a number of basic types.

A basic type is one that refers to a simple value, such as an int or a string. This contrasts with a QML Object Types, which refers to an object with properties, signals, methods and so on. Unlike an object type, a basic type cannot be used to declare QML objects: it is not possible, for example, to declare an int{} object or a size{} object.

Basic types can be used to refer to:

  • A single value (e.g. int refers to a single number, var can refer to a single list of items)
  • A value that contains a simple set of property-value pairs (e.g. size refers to a value with width and height attributes)

When a variable or property holds a basic type and it is assigned to another variable or property, then a copy of the value is made. In JavaScript, this value is called a primitive value.

Supported Basic Types

Some basic types are supported by the engine by default and do not require an import statement to be used, while others do require the client to import the module which provides them. All of the basic types listed below may be used as a property type in a QML document, with the following exceptions:

  • list must be used in conjunction with a QML object type
  • enumeration cannot be used directly as the enumeration must be defined by a registered QML object type

Basic Types Provided By The QML Language

The basic types supported natively in the QML language are listed below:

 

bool

Binary true/false value

double

Number with a decimal point, stored in double precision

enumeration

Named enumeration value

int

Whole number, e.g. 0, 10, or -20

list

List of QML objects

real

Number with a decimal point

string

Free form text string

url

Resource locator

var

Generic property type

Basic Types Provided By QML Modules

QML modules may extend the QML language with more basic types. For example, the basic types provided by the QtQuick module are listed below:

 

date

Date value

point

Value with x and y attributes

rect

Value with x, y, width and height attributes

size

Value with width and height attributes

The Qt global object provides useful functions for manipulating values of basic types.

Currently only QML modules which are provided by Qt may provide their own basic types, however this may change in future releases of Qt QML. In order to use types provided by a particular QML module, clients must import that module in their QML documents.

Property Change Behavior for Basic Types

Some basic types have properties: for example, the font type has pixelSize, family and bold properties. Unlike properties of object types, properties of basic types do not provide their own property change signals. It is only possible to create a property change signal handler for the basic type property itself:

 Text {
     // invalid!
     onFont.pixelSizeChanged: doSomething()
 
     // also invalid!
     font {
         onPixelSizeChanged: doSomething()
     }
 
     // but this is ok
     onFontChanged: doSomething()
 }

Be aware, however, that a property change signal for a basic type is emitted whenever any of its attributes have changed, as well as when the property itself changes. Take the following code, for example:

 Text {
     onFontChanged: console.log("font changed")
 
     Text { id: otherText }
 
     focus: true
 
     // changing any of the font attributes, or reassigning the property
     // to a different font value, will invoke the onFontChanged handler
     Keys.onDigit1Pressed: font.pixelSize += 1
     Keys.onDigit2Pressed: font.b = !font.b
     Keys.onDigit3Pressed: font = otherText.font
 }

In contrast, properties of an object type emit their own property change signals, and a property change signal handler for an object-type property is only invoked when the property is reassigned to a different object value.

See also The QML Type System.

© 2020 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值