QML非常经典的代码.docx
- 文档编号:6976527
- 上传时间:2023-01-14
- 格式:DOCX
- 页数:43
- 大小:26.07KB
QML非常经典的代码.docx
《QML非常经典的代码.docx》由会员分享,可在线阅读,更多相关《QML非常经典的代码.docx(43页珍藏版)》请在冰豆网上搜索。
QML非常经典的代码
1.介绍
QML是一种描述语言,主要是对界面效果等的一种描述,它可以结合javaScript来进行更复杂的效果及逻辑实现。
比如做个游戏,实现一些更有趣的功能等
2.简单的例子
importQt4.7
Rectangle{
width:
200
height:
200
color:
"blue"
}
代码是绘制一个蓝色的矩形,宽200高200,import包含一个qt4.7的包
3.基本元素的介绍(自己翻译意思会有出入,敬请见谅)
基本可视化项
Item基本的项元素在QML中所有可视化的向都继承他
Rectangle基本的可视化矩形元素
Gradient定义一个两种颜色的渐变过程
GradientStop定义个颜色,被Gradient使用
Image在场景中使用位图
BorderImage(特殊的项)定义一张图片并当做边界
AnimatedImage为播放动画存储一系列的帧
Text在场景中使用文本
TextInput显示可编辑为文本
IntValidatorint验证器
DoubleValidatordouble验证器
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包含可视化项(visualitems)到一个view中,相当是一个容器
VisualDataModel包含一个model和一个delegate,model包含需要的数据,delegate设计显示的项的信息,具体的去看例子
Package他的目的是把VisualDataModel共享给多个view,具体还要学习
XmlListModel特殊的一个模式使用XPath表达式,使用xml来设置元素,参考例子
XmlRoleXmlListModel的一个特殊的角色
试图
ListView提供一个链表显示模型试图
GridView提供一个网格显示模型试图
PathView提供一个内容沿着路径来显示的模型
Path定义一个PathView使用的轨迹
PathLine定义一个线性的轨迹
PathQuad定义一个二次贝塞尔曲线的轨迹
PathCubic定义一个三次贝塞尔曲线的轨迹
PathAttribute允许绑定一个属性上,具体看例子
PathPercent修改item分配的轨迹不是很明了其中的意思
WebView允许添加网页内容到一个canvas上
定位器
Column整理它的子列(纵)
Row整理它的子行(横)
Grid设置它的子到一个网格上
Flow目的是不让他的子项重叠在一起
实用
Connections明确连接信号和信号句柄
Component封装QMLitems想一个组件一样
Timer提供时间触发器
QtObject基本的元素只包含objectName属性
Qtqml全局Qtobject提供使用的枚举和函数
WorkerScript允许在QML使用线程
Loader控制载入item或组件
Repeater使用一个模型创建多个组件
SystemPalette为Qtpalettes提供一个通道
FontLoader载入字体根据名字或URL
LayoutItem允许声明UI元素插入到qtGraphicsView布局中
变换
Scale分派item缩放行为
Rotation分派item旋转行为
Translate分派item移动行为
4.基本元素的使用例子
1.位置是0,0宽高分别是200
Item{
x:
0;y:
0;
width:
200;height:
200;
}
2.位置是:
0,0宽高分别是200,颜色是红色
Rectangle{
x:
0;y:
0;
width:
200;height:
200;
color:
"red"
}
3.分别在总高度的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{
source:
"../Images/button1.png"
}
5.他将一张图片分成9部分
当图片进行缩放的时候
A.1379位置的都不会进行缩放
B.28将根据属性horzontalTileMode进行缩放
C.46将根据属性verticalTileMode进行缩放
D.5将根据属性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.主要用于播放gif图片
Rectangle{
width:
animation.width;height:
animation.height+8
AnimatedImage{id:
animation;source:
"animation.gif"}
Rectangle{
propertyintframes:
animation.frameCount
width:
4;height:
8
x:
(animation.width-width)*animation.currentFrame/frames
y:
animation.height
color:
"red"
}
}
7.显示文本(具体的其他设置请看文档)
Text{
text:
"text"
}
8.下面是设置一个输入文本框,框中的字符串是Text,并设置鼠标可以选择文本
TextInput{
text:
"Text"
selectByMouse:
true;//鼠标可以选择
}
9.int型验证器,和输入框结合后就是只能输入整型数据
TextInput{
//最高可以输入100,最低输入10
IntValidator{id:
intval;bottom:
10;top:
100;}
width:
100;height:
20;
text:
"";
//使用校验器
validator:
intval;
}
10.只能输入浮点数
TextInput{
//最高可以输入100,最低输入10decimals最多有多少位小数
//notation表示是使用科学计数法还是(默认),还是直接的小数当获取里面的数据
DoubleValidator{id:
intval;decimals:
4;bottom:
10;top:
100;notation:
DoubleValidator.StandardNotation}
width:
100;height:
20;
text:
"";
//使用校验器
validator:
intval;
}
11.使用正则表达式
TextInput{
//使用一个正则表达式来控制输入的字符串
///^[a-zA-Z]{1}[0-1]{0,2}[a-z]{1,3}$/表示开始位置必须是一个大写或小写字母
//接下来是0~2个的数字而且是0或1,在接下来是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.
显示一段helloworld的html文本和相同
TextEdit{
width:
240
text:
"HelloWorld!
"
font.family:
"Helvetica"
font.pointSize:
20
color:
"blue"
focus:
true
}
13.
主要是用来判断鼠标事件的区域
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.
不是很清楚说的什么,好像是说同一个时刻只有一个item有焦点
15.
显示一个200x200的框,框中显示图片上200x200的部分
Flickable{
width:
200;height:
200
//设置使用图片的宽高,而现实的是200x200的现实框
contentWidth:
image.width;contentHeight:
image.height
Image{id:
image;source:
"../Images/need.png"}
}
16.
包含两个面,一个前面,一个后面,实现一个控件前后的翻转效果,并且在后面可以添加一些控制
Flipable{
id:
flipable
width:
240
height:
240
propertyintangle:
0
propertyboolflipped:
false
front:
Image{source:
"front.png"}//前面
back:
Image{source:
"back.png"}//后面
//旋转动画前后面交换
transform:
Rotation{
origin.x:
flipable.width/2;origin.y:
flipable.height/2
axis.x:
0;axis.y:
1;axis.z:
0//rotatearoundy-axis
angle:
flipable.angle
}
states:
State{
name:
"back"
PropertyChanges{target:
flipable;angle:
180}
when:
flipable.flipped
}
transitions:
Transition{
NumberAnimation{properties:
"angle";duration:
1000}
}
MouseArea{
anchors.fill:
parent
onClicked:
flipable.flipped=!
flipable.flipped
}
}
17.
//当鼠标按下后改变myRect的颜色
Rectangle{
id:
myRect
width:
100;height:
100
color:
"black"
MouseArea{
id:
mouseArea
anchors.fill:
parent
onClicked:
myRect.state=='clicked'?
myRect.state="":
myRect.state='clicked';
}
//设置状态
states:
[
State{
name:
"clicked"
PropertyChanges{target:
myRect;color:
"red"}
18.
//当鼠标按下后改变状态
//状态里面的属性改变包含了文本和颜色的改变
Text{
id:
myText
width:
100;height:
100
text:
"Hello"
color:
"blue"
states:
State{
name:
"myState"
//当这个状态被设置的时候,将改变myText的文本和颜色
PropertyChanges{
target:
myText
text:
"Goodbye"
color:
"red"
}
}
MouseArea{anchors.fill:
parent;onClicked:
myText.state='myState'}
19.一个状态组中可以包含很多的状态和变化,而状态也可以和变换绑定.
20.在状态中可以对脚本中的函数进行调用
//Sc.js
functionchangeColor()//返回蓝色
{
return"blue";
}
//test.qml
import"Sc.js"asCode
Rectangle{
id:
rect
width:
50;height:
50
color:
"red"
MouseArea{
anchors.fill:
parent
onClicked:
rect.state="first"//鼠标按下改变状态
}
states:
State{name:
"first";
StateChangeScript{
name:
"myScript";
script:
rect.color=Code.changeColor();
21.把指定的item换一个item父节点
Item{
width:
200;height:
100
Rectangle{
id:
redRect
width:
100;height:
100
color:
"red"
}
//本来blueRect的父节点是Item当鼠标按下后他被设置到redRect上
Rectangle{
id:
blueRect
x:
redRect.width
width:
50;height:
50
color:
"blue"
states:
State{
name:
"reparented"
//改变父节点
ParentChange{target:
blueRect;parent:
redRect;x:
10;y:
10}
}
MouseArea{anchors.fill:
parent;onClicked:
blueRect.state="reparented"}
}
}
22.
Rectangle{
id:
window
width:
120;height:
120
color:
"black"
Rectangle{id:
myRect;width:
50;height:
50;color:
"red"}
states:
State{
name:
"reanchored"
AnchorChanges{//改变myRect的anchors属性
target:
myRect
anchors.top:
window.top
anchors.bottom:
window.bottom
}
PropertyChanges{
target:
myRect
anchors.topMargin:
10
anchors.bottomMargin:
10
}
}
//鼠标事件
MouseArea{anchors.fill:
parent;onClicked:
window.state="reanchored"}
23Rectangle{
id:
rect
width:
100;height:
100
color:
"red"
//针对宽度的动画
Behavioronwidth{
NumberAnimation{duration:
1000}
}
MouseArea{
anchors.fill:
parent
onClicked:
rect.width=50
}
24.
串行播放多个动画
Rectangle{
id:
rect1
width:
500;height:
500
Rectangle{
id:
rect;
color:
"red"
width:
100;height:
100
//串行播放多个动画,先横向移动,在纵向移动
SequentialAnimation{
running:
true;
NumberAnimation{target:
rect;properties:
"x";to:
50;duration:
1000}
NumberAnimation{target:
rect;properties:
"y";to:
50;duration:
1000}
}
25.
Rectangle{
id:
rect1
width:
500;height:
500
Rectangle{
id:
rect;
color:
"red"
width:
100;height:
100
//并行播放动画,同时横向和纵向移动
ParallelAnimation{
running:
true;
NumberAnimation{target:
rect;properties:
"x";to:
50;duration:
1000}
NumberAnimation{target:
rect;properties:
"y";to:
50;duration:
1000}
}
}
}
26.
Rectangle{
id:
rect
width:
100;height:
100
color:
"red"
states:
State{
name:
"moved"
PropertyChanges{target:
rect;x:
50}
}
transitions:
Transition{
//属性动画这里是当属性x或y发生变化的时候,就播放这样一个动画
PropertyAnimation{properties:
"x,y";easing.type:
Easing.InOutQuad}
}
MouseArea{
anchors.fill:
parent;
onClicked:
rect.state="moved";
}
}
27.
Rectangle{
width:
100;height:
100
color:
"red"
//对当前item的x进行移动,目标移动到x=50
NumberAnimationonx{to:
50;duration:
1000}
}
28.
29.
颜色的过度
Rectangle{
width:
100;height:
100
color:
"red"
ColorAnimationoncolor{to:
"yellow";duration:
1000}
}
30.
默认是绕z轴进行的旋转
Item{
width:
300;height:
300
Rectangle{
id:
rect
width:
150;height:
100;anchors.centerIn:
parent
color:
"red"
smooth:
true
states:
State{
name:
"rotated";PropertyChanges{target:
rect;rotation:
180}
}
transitions:
Transition{
RotationAnimation{duration:
1000;direction:
RotationAnimation.Counterclockwise}
}
}
MouseArea{anchors.fill:
parent;onClicked:
rect.state="rotated"}
}
31.
一个切换父节点的动画,平滑的过度
Item{
width:
200;height:
100
Rectangle{
id:
redRect
width:
100;height:
100
color:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- QML 非常 经典 代码