QML 基本内容介绍

本文档详细介绍了QML的基本元素,包括Item、Rectangle、Gradient、Image等可视化项,以及MouseArea、State、Transition等交互元素。通过实例展示了如何创建和使用这些元素,如矩形、渐变、图像、文本输入、动画效果等,帮助读者掌握QML的界面设计和交互实现。
摘要由CSDN通过智能技术生成
1. 介绍

QML是一种描述语言,主要是对界面效果等的一种描述,它可以结合javaScript来进行更复杂的效果及逻辑实现。比如做个游戏,实现一些更有趣的功能等


2. 简单的例子

import Qt 4.7

Rectangle {

width: 200

height: 200

color: "blue"

}

代码是绘制一个蓝色的矩形,宽 200 200import包含一个qt4.7的包

3.基本元素的介绍(自己翻译意思会有出入,敬请见谅)

基本可视化项

Item 基本的项元素 在QML中所有可视化的向都继承他

Rectangle 基本的可视化矩形元素

Gradient 定义一个两种颜色的渐变过程

GradientStop 定义个颜色,被Gradient使用

Image 在场景中使用位图

BorderImage(特殊的项) 定义一张图片并当做边界

AnimatedImage 为播放动画存储一系列的帧

Text 在场景中使用文本

TextInput 显示可编辑为文本

IntValidator int 验证器

DoubleValidator double 验证器

RegExpValidator 验证字符串正则表达式

TextEdit 显示多行可编辑文本

基本的交互项

MouseArea 鼠标句柄交互

FocusScope 键盘焦点句柄

Flickable 提供一种浏览整张图片的一部分的效果,具体看例子

Flipable 提供一个平面,可以进行翻转看他的前面或后面,具体看例子

状态

State 定义一个配置对象和属性的集合

PropertyChanges 使用一个State描述属性的改变

StateGroup 包含一个状态集合和状态变换

ParentChange 重新定义父集,也就是换个父节点

AnchorChanges 在一个状态中改变anchors

动画和变换

Behavior 默认的属性变换动画

SequentialAnimation 对定义的动画串行播放

ParallelAnimation 对定义的动画并行播放

PropertyAnimation 属性变换动画

NumberAnimation 对实数类型属性进行的动画

Vector3dAnimation QVector3d进行的属性

ColorAnimation 颜色进行的变换动画

RotationAnimation 对旋转进行的变换动画

ParentAnimation 对父节点进行变换的动画,改变绑定的父节点

AnchorAnimation anchor 进行改变的动画

PauseAnimation 延迟处理

SmoothedAnimation 允许属性平滑的过度

SpringAnimation 一种加速的效果

PropertyAction 允许在动画过程中对属性的直接改变

ScriptAction 允许动画过程中调用脚本

Transition 在状态变换中加入动作变化

工作中的数据

Binding 在创建的时候绑定一些数据到一些属性

ListModel 定义链表数据

ListElement 定义ListModel的一个数据项

VisualItemModel 包含可视化项(visual items)到一个view中,相当是一个容器

VisualDataModel 包含一个model和一个delegatemodel包含需要的数据,delegate设计显示的项的信息,具体的去看例子

Package 他的目的是把VisualDataModel共享给多个view,具体还要学习

XmlListModel 特殊的一个模式使用XPath表达式,使用xml来设置元素,参考例子

XmlRole XmlListModel的一个特殊的角色

视图

GridView 提供一个网格显示模型视图

ListView 提供一个链表显示模型视图

PathView 提供一个内容沿着路径来显示的模型

Path 定义一个PathView使用的轨迹

PathLine 定义一个线性的轨迹

PathQuad 定义一个二次贝塞尔曲线的轨迹

PathCubic 定义一个三次贝塞尔曲线的轨迹

PathAttribute 允许绑定一个属性上,具体看例子

PathPercent 修改item分配的轨迹 不是很明了其中的意思

WebView 允许添加网页内容到一个canvas

定位器

Column 整理它的子列()

Row 整理它的子行()

Grid 设置它的子到一个网格上

Flow 目的是不让他的子项重叠在一起

实用

Connections 明确连接信号和信号句柄

Component 封装QML items 想一个组件一样

Timer 提供时间触发器

QtObject 基本的元素只包含objectName属性

Qt qml全局Qt object提供使用的枚举和函数

WorkerScript 允许在QML使用线程

Loader 控制载入item或组件

Repeater 使用一个模型创建多个组件

SystemPalette Qt palettes提供一个通道

FontLoader 载入字体根据名字或URL

LayoutItem 允许声明UI元素插入到qtGraphicsView 布局中

变换

Scale 分派item 缩放行为

Rotation 分派item 旋转行为

Translate 分派item 移动行为



4.基本元素的使用例子

1. Item位置是0,0 宽高分别是200

Item {

x: 0; y: 0;

width: 200; height: 200;

}

2. Rectangle位置是:0,0宽高分别是200, 颜色是红色

Rectangle {

x: 0; y: 0;

width: 200; height: 200;

color: "red"

}

3. Gradient GradientStop 分别在总高度的0 颜色红色 总高度的1/3 黄色 总高度的1是绿色

Rectangle {

width: 100; height: 100

gradient: Gradient {

GradientStop { position: 0.0; color: "red" }

GradientStop { position: 0.33; color: "yellow" }

GradientStop { position: 1.0; color: "green" }

}

}

4. Image 设置一张图片

Image {

source: "../Images/button1.png"

}

5. BorderImage 他将一张图片分成9部分

当图片进行缩放的时候

A1 3 7 9 位置的都不会进行缩放

B. 2 8将根据属性horzontalTileMode 进行缩放

C4 6 将根据属性verticalTileMode 进行缩放

D5 将根据属性horzontalTileMode verticalTileMode 进行缩放

BorderImage {

width: 180; height: 180

// 分割1~9块的4个点是根据border设置的坐标来实现的

// 本别是距离坐标 上边 右边 下边的距离

border { left: 30; top: 30; right: 30; bottom: 30 }

horizontalTileMode: BorderImage.Stretch

verticalTileMode: BorderImage.Stretch

source: "../Images/button1.png"

}


6. AnimatedImage 主要用于播放gif图片

Rectangle {

width: animation.width; height: animation.height + 8

AnimatedImage { id: animation; source: "animation.gif" }

Rectangle {

property int frames: animation.frameCount

width: 4; height: 8

x: (animation.width - width) * animation.currentFrame / frames

y: animation.height

color: "red"

}

}

7. Text 显示文本(具体的其他设置请看文档)

Text {

text: "text"

}

8. TextInput 下面是设置一个输入文本框,框中的字符串是Text, 并设置鼠标可以选择文本

TextInput{

text: "Text"

selectByMouse: true; // 鼠标可以选择

}

9IntValidator int 型验证器,和输入框结合后就是只能输入整型数据

TextInput{

// 最高可以输入100 最低输入10

IntValidator{ id: intval; bottom: 10; top: 100;}

width: 100; height: 20;

text: "";

// 使用校验器

validator: intval;

}

10. DoubleValidator 只能输入浮点数

TextInput{

// 最高可以输入100 最低输入10 decimals最多有多少位小数

// notation 表示是使用科学计数法还是(默认),还是直接的小数 当获取里面的数据

DoubleValidator{ id: intval; decimals: 4; bottom: 10; top: 100; notation:DoubleValidator.StandardNotation}

width: 100; height: 20;

text: "";

// 使用校验器

validator: intval;

}

11. RegExpValidator 使用正则表达式

TextInput{

// 使用一个正则表达式来控制输入的字符串

// /^[a-zA-Z]{1}[0-1]{0,2}[a-z]{1,3}$/ 表示 开始位置必须是一个大写或小写字母

// 接下来是0~2个的数字而且是01,在接下来是1~3个的小写字母

RegExpValidator{ id: intval; regExp:/^[a-zA-Z]{1}[0-1]{0,2}[a-z]{1,3}$/;}

width: 100; height: 20;

text: "";

// 使用校验器

validator: intval;

}

12. TextEdit

显示一段hello worldhtml文本和TextInput相同


TextEdit {

width: 240

text: "<b>Hello</b> <i>World!</i>"

font.family: "Helvetica"

font.pointSize: 20

color: "blue"

focus: true

}


13. MouseArea


主要是用来判断鼠标事件的区域

Rectangle{

x: 0; y: 0;

width: 100; height:100;

Rectangle{

id: mousrect

x: 20; y: 20;

width: 20; height: 20;

color: "blue"

MouseArea{

// 使用父的区域作为鼠标判断的区域及 x: 20; y: 20; width: 20; height: 20;

anchors.fill: parent;

// 但鼠标按下后 mousrect变成红色,当鼠标松开后变成蓝色

onPressed: { mousrect.color = "red";}

onReleased: { mousrect.color = "blue";}

}

}

}



14. FocusScope

不是很清楚说的什么,好像是说同一个时刻只有一个item有焦点

15. Flickable

显示一个200x200的框,框中显示图片上200x200的部分

Flickable {

width: 200; height: 200

// 设置使用图片的宽 高,而现实的是 200x200的现实框

contentWidth: image.width; contentHeight: image.height

Image { id: image; source: "../Images/need.png" }

}


16. Flipable

包含两个面,一个前面,一个后面,实现一个控件前后的翻转效果,并且在后面可以添加一些控制


Flipable {

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值