QML基本数据类型介绍
参考资料
https://doc.qt.io/qt-5/qmlbasictypes.html
一、总体说明
QML有许多基本类型,例如整型int或字符串类型string,基本类型不能用于声明QML对象,例如不能声明 int{}对象或size{}对象。
基本类型可用于定义:
1、单个值(例如,int 表示单个数字,var 可以指单个项目列表)
2、包含一组简单的属性值对(例如size 包含 width 和 height 两个简单的键值)
注意:默认情况下,系统支持基本类型,并且不需要使用import语句,导入模块,而其他类型则需要客户端导入模块才能使用。
二、基本型的详细说明
下面列出了QML语言本身支持的基本类型:
布尔值 true/false | |
double | 双精度数字类型 |
enumeration | 枚举 |
int | 整型 |
list | QML 对象列表 |
real | 实型(C++中 float) |
string | 字符串类型 |
url | 资源定位器 |
var | 通用属性类型 |
2.1、bool
布尔型只有true/false两个值
Item {
focus: true
clip: false
}
2.2、double
定义双精度数据
Item {
property double number: 32155.2355
}
2.3、enumeration
枚举类型
Text { horizontalAlignment: Text.AlignRight }
2.4、int
整型数据,范围-2000000000~2000000000
Item { width: 100; height: 200 }
2.5、list
列表数据
Item {
width: 100; height: 100
states: [
State { name: "activated" }, //列表数据
State { name: "deactivated" }
]
Component.onCompleted: {
console.log("Name of first state:", states[0].name)
for (var i = 0; i < states.length; i++)
console.log("state", i, states[i].name)
}
}
2.6、real
定义实数类型
Item { width: 100.45; height: 150.82 }
2.7、string
Text { text: "Hello world!" }
2.8、url
定义资源的路径/位置
Image { source: "pics/logo.png" }
2.9、var
通用的属性,可以涉及任何数据类型,类似于c语言的void类型。
Item {
property var aNumber: 100
property var aBool: false
property var aString: "Hello world!"
property var anotherString: String("#FF008800")
property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
property var aRect: Qt.rect(10, 10, 10, 10)
property var aPoint: Qt.point(10, 10)
property var aSize: Qt.size(10, 10)
property var aVector3d: Qt.vector3d(100, 100, 100)
property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
property var anObject: { "foo": 10, "bar": 20 }
property var aFunction: (function() { return "one"; })
}
2.10、variant
通用的属性,可以涉及任何数据类型,为了兼容老版本而定义,新的程序不要用这个,应该使用var定义。
Item {
property variant aNumber: 100
property variant aString: "Hello world!"
property variant aBool: false
}
Date | 日期值 |
point | 具有x和y属性坐标值 |
rect | 具有 x,y,width,height属性的值 |
size | 具有 width 和 height 属性的值 |
3.1、Date
设置日期和时间
Item {
Timer {interval: 500; running: true; repeat: true;
onTriggered: time.text = Qt.formatDateTime(new Date(), "dddd\nyyyy-MM-dd\nhh-mm-ss") // 星期年月日时分秒
}
Text { id: time }
}
3.2、point
定义x和y的坐标
CustomObject { myPointProperty: "0,20" }
3.3、rect
定义范围,包括x和y坐标及宽和高。
Rectangle {
width: childrenRect.width
height: childrenRect.height
Rectangle { width: 100; height: 100 }
}
CustomObject { myRectProperty: Qt.rect(50, 50, 100, 100) }
3.4、size
描述长和宽的值
Image { sourceSize: "150x50" }
3.5、color型
设置颜色属性,颜色的设置有3种方式
Text {
color: "red" //设置为红色字体
}
Rectangle {
color: "#800000FF" // AARRGGBB fully transparent
y: 120; width: 40; height: 40
}
Rectangle {
color: "#FF0000" // RRGGBB fully transparent
}
3.6、coordinate
coordinate类表示地理位置,经、纬、高度以及距离和方位角的计算等
Location { coordinate: QtPositioning.coordinate(-27.5, 153.1) }
3.7、font
字体属性
string font.family // 字体的名字,字体类型
bool font.bold // 是否使用粗体
bool font.italic // 是否使用斜体
bool font.underline // 是否使用下划线
real font.pointSize // 设定字体大小 (1pt = 0.3527mm)
int font.pixelSize // 设定字体大小, 当 pointSize 和 pixelSize 都设定了,那么使用pixelSize
enumeration font.weight // 设置文本的粗细 (可以选择的范围是0-99)
bool font.overline // 设置文字上方是否划线
bool font.strikeout // 设置文字是否使用strikeout 风格
enumeration font.capitalization // 设定文字的大小写属性,(如果有大小写之分的话)
real font.letterSpacing // 修改默认的字符间隔,可以是负数,如果是负数减少间隔
real font.wordSpacing //修改默认的词间隔,可以是负数,如果是负数减少间隔
font.MixedCase - 按照正常显示,不修改大小写
font.AllUppercase - 所有都大写
font.AllLowercase - 所有都小写
font.SmallCaps - 修改首字母为小写
font.Capitalize - 修改首字母为大写
3.8、geocircle、geopath、geopolygon、georectangle、geoshape
获得地理坐标相关的内容,专业性强,请参考官网资料。