QML现学现用

QML 与 C++混合开发

  • Qt Quick QML负责UI
  • C++负责逻辑与数据处理
  • 交互使用Qt的Signal & Slots

QML的基本概念 元素和属性

Rectangle 矩形元素

import QtQuick 2.2
Rectangle {
 width: 300
 height: 200
 opacity: 0.5 //透明度
 color: "red"
 Text {
 anchors.horizontalCenter: parent.horizontalCenter
 anchors.verticalCenter: parent.verticalCenter
 text: "Hello World"
 }

//偏移
 Rectangle {
 color: "blue"
 x: 50; y: 50; width: 100; height: 100
 }
}

为了自适应不同的设备,可以使用
anchors.horizontalCenter
anchors.horizontalCenter: pic.horizontalCenter
anchors.top: pic.bottom
anchors.topMargin: 5

import QtQuick 2.2
//Rectangle 高级应用
Rectangle {
 property alias text: textItem.text  //属性别名

width: 100; height: 30
 border.width: 1
 radius: 5
 smooth: true //圆弧

gradient: Gradient {  //渐进变化
 GradientStop { position: 0.0; color: "darkGray" }
 GradientStop { position: 0.5; color: "black" }
 GradientStop { position: 1.0; color: "darkGray" }
 }

Text {  //文本
 id: textItem
 anchors.centerIn: parent
 font.pointSize: 20
 color: "white"
 }

}

MouseArea 交互的元素
- Flickable
- FocusScope
-

MouseArea 元素使用 onClicked属性
当然除了onClicked, 还有其他属性onEntered、onExited、onPressed、onReleased

import QtQuick 2.2

//element parent
Rectangle {
 color: "#ff0000"
 width: 310
 height: 210


//element block  define as the mousearea
 Rectangle {
 id: block
 width: 60
 height: 40
 anchors.horizontalCenter: parent.horizontalCenter
 anchors.verticalCenter: parent.verticalCenter

Text {
 anchors.horizontalCenter: parent.horizontalCenter
 anchors.verticalCenter: parent.verticalCenter
 text: "block"
    }
 }

//Interaction
 MouseArea {
 anchors.fill: block  //定义区域
 onClicked: {
 if (parent.color == "#ff0000") 
        parent.color = "#ff9900";
 else 
        parent.color = "#ff0000";

            }
 }

}

为了提升交互体验,要有适当的过渡 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一銤阳光

希望分享的内容对你有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值