qml学习2(一些基础控件)
1、首先是图片,图片资源最好在同级目录下创建一个文件夹,放入对应图片,然后再使用
点击查看代码
Image {
// anchors.fill:parent
// fillMode: Image.PreserveAspectFit//填充方式,全部填充,保持原图片大小
//fillMode: Image.Stretch//拉伸模式
source: "sourse/AZS.png"
sourceSize: Qt.size(400,200)//提前进行压缩,防止图片过大消耗内存资源
asynchronous: true//异步加载,用于图片资源放于网络上
}
点击查看代码
Text {
text: qsTr("hello")
font.family: "微软雅黑"
font.pixelSize: 40
font.italic: true//斜体
font.underline: true//下划线
color:"lightblue"
wrapMode: Text.NoWrap//不跨行
width:100
height: 100
anchors.horizontalCenter: parent.horizontalCenter//居中
}
点击查看代码
//用矩形可以方便带边框
Rectangle
{
x:20
y:50
width: 300
height: 400
color:"grey"//矩形的背景颜色
radius:5//圆角边框(大小)
border.color: "red"//边框颜色
clip: true//超出部分裁剪掉
//可编辑的文本框
TextEdit
{
width: 300
height: 400
text: "请输入文本"
font.pixelSize: 20
color: "lightblue"
focus: true;//上来确定焦点
font.italic: true//斜体
clip: true//超出部分裁剪掉
}
}
点击查看代码
Button
{
text: "点击我"
font.pixelSize: 30
anchors.centerIn: parent
checkable: true//可以改变为被选中这个状态
onCheckedChanged:
{
console.log("状态",checked);//编译输出
if(checked)
{
text="被点击"
}
else
{
text="点击我点击我"
}
}
}
点击查看代码
Button
{
id:mybtn
text: "点击我"
font.pixelSize: 30
anchors.centerIn: parent
checkable: true//可以改变为被选中这个状态
//点击后操作
onCheckedChanged:
{
console.log("状态",checked);//编译输出
if(checked)
{
text="被点击"
}
else
{
text="点击我点击我"
}
}
//定义一个新内容项目来改变字体颜色
contentItem: Text {
text: mybtn.text
color:"white"
font.pixelSize: 30
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
//定义一个背景来改变按钮颜色
background: Rectangle
{
radius:5
//悬浮下是红色,点击后是绿色,未点击是蓝色
color: mybtn.checked ? "lightgreen"
: mybtn.hovered ? "red"
: "blue"
border.color:"red"
}
}
点击查看代码
Button
{
width: 50
height: 50
icon.source: "qrc:/source/ALS.png"
anchors.centerIn: parent//居中
background:Rectangle
{
color:"green"
}
onPressed: timer.start()
onReleased: timer.stop()
Timer
{
id:timer
interval: 1000
repeat:true
onTriggered: console.log("长按了")
}
}
点击查看代码
TextField
{
anchors.centerIn: parent//居中
color:"white"
width: 200
height: 200
placeholderText: "请输入文本"
onAccepted: console.log("输入了",text)//回车后会触发
echoMode:TextInput.Password//输入模式为密码模式
maximumLength:8//最大文本长度
background:Rectangle
{
radius:8//圆角
color:"lightblue"
border.color:"grey"
border.width:2
}
}
点击查看代码
ScrollView//方便装入滚动条
{
anchors.centerIn: parent//居中
width: 100
height: 100
TextArea
{
width: 100
height: 100
placeholderText: "请输入文本"
color:"white"
wrapMode:TextArea.Wrap//允许跨行
onTextChanged:console.log(text)//文本改变后触发
background:Rectangle
{
radius:8//圆角
color:"lightblue"
border.color:"grey"
border.width:2
}
}
}
点击查看代码
//滑块部分
Slider
{
id:mySlider
anchors.centerIn: parent
width: 200
//范围从0到200
from:0
to:200
stepSize: 5//每次滑动结果是5的倍数
value:50//滑块初始位置为50
onValueChanged:
{
console.log("当前值为",value)
}
}
Text {
anchors.top: mySlider.bottom
anchors.horizontalCenter: mySlider.horizontalCenter
padding: 10//控件间隔
color: "red"
font.pixelSize: 40
text: Math.round(mySlider.value)
}
点击查看代码
ProgressBar
{
id:myProgress
width: 200
anchors.centerIn: parent
from: 0
to:100
value: 20
}
Timer
{
id:mytimer
interval: 100
repeat: true//允许重复计时
running: true//设置启动
onTriggered:
{
if(myProgress.value<myProgress.to)
myProgress.value+=5
else
myProgress.value=0
}
}
浙公网安备 33010602011771号