Qt开发QTQuick
前言
QT Quick 和Qt widgets 这两种技术,官方是强推QT Quick 的。
QT Quick中布局一般有如下四种方式, 绝对坐标:x、y、z、width、height、top、left 锚(anchors) 布局 定位器(Row、Column、Grid、Flow) 布局管理器(RowLayout、ColumnLayout、GridLayout、StackLayout)
绝对布局很好理解,给值就显示,但是不灵活;
anchors 实际上是 Item 的一个属性集
Row 则是一个单独的 Item ,专门用来管理其它 Item 的,后面介绍的几种布局,也是类似的。
锚(anchors) 布局的参数: //左上右下对齐 anchors.left : AnchorLine anchors.top : AnchorLine anchors.right : AnchorLine anchors.bottom : AnchorLine //Margin anchors.leftMargin : real anchors.topMargin : real anchors.rightMargin : real anchors.bottomMargin : real anchors.margins : real //基线对齐及偏移 anchors.baseline : AnchorLine anchors.baselineOffset : real anchors.mirrored : bool anchors.fill : Item //居中与偏移 anchors.centerIn : Item anchors.horizontalCenter : AnchorLine anchors.verticalCenter : AnchorLine anchors.horizontalCenterOffset : real anchors.verticalCenterOffset : real
其中 real 具体的数值 Item是组建的ID或者parent bool是true或false AnchorLine 示例 anchors.horizontalCenter: parent.horizontalCenter
注意
不要在Row或RowLayout相关的组件中使用anchors,会导致组件本身的特性不生效。 窗口设置
窗口的属性 Window { title: qsTr("一个普通标题窗口") //窗口标题 width: 640 //宽度 height: 480 //高度 visible: true //是否可见,缺省为true color: "#ffffff" //窗口背景色 //#00000000 为窗口透明 //QML支持black 等颜色样式(没有#) //QML支持#11cfff 等颜色样式 //QML同样支持RGB格式 flags: Qt.Window //窗口标志 说明是什么窗口 使用 | 分割,缺省为Qt.Window //Qt.Window 普通窗口模式,带标题栏 //Qt.FramelessWindowHint 隐藏标题栏窗口 opacity: 1 //透明度 数值区间为0~1 支持小数,缺省为1 x:0 //位于父窗体的x位置,以左上角为起点,缺省为0 (此时window的父窗体就是桌面了) y:0 //位于父窗体的y位置,以左上角为起点,缺省为0 (此时window的父窗体就是桌面了) }
无边框 Window { width: 640 height: 480 visible: true color: "#fefefe" title: qsTr("主页面") flags: "FramelessWindowHint" }
显示标题栏,但是没有关闭最大最小化按钮 Window { width: 640 height: 480 visible: true color: "#fefefe" title: qsTr("主页面") flags: "CustomizeWindowHint" }
背景透明无边框窗口 Window { width: 640 height: 480 visible: true color: "#00000000" title: qsTr("主页面") flags: Qt.FramelessWindowHint opacity:1 }
opacity这个属性是对当前组件以及子组件都设置不透明度,所以不太适用
color: Qt.rgba(0,0,0,0) 是对当前设置透明度,不会传到子组件组件
基本组件
这里面的这几个内部也可以填充其它组件 MouseArea Rectangle
定位组件和布局管理器
定位器(Row、Column、Grid、Flow)
布局管理器(RowLayout、ColumnLayout、GridLayout、StackLayout) Layout
要使用layout布局的属性 需要引用 import QtQuick.Layouts 1.12示例1
一个简单的示例
横向分布,最后一个填充剩余空间。 import QtQuick 2.14 import QtQuick.Window 2.14 import QtQuick.Layouts 1.12 Window { width: 640 height: 480 visible: true color: "#f3f3f3" title: qsTr("主页面") RowLayout { id: row height: 200 spacing: 0 anchors.left:parent.left anchors.right:parent.right Rectangle { id: rectangle width: 200 height: parent.height color: "red" } Rectangle { id: rectangle2 width: 200 height: parent.height color: "green" } Rectangle { id: rectangle3 height: parent.height color: "blue" Layout.fillWidth: true } } }
显示效果
其中 RowLayout { id: row height: 200 spacing: 0 anchors.left:parent.left anchors.right:parent.right }
和 RowLayout { id: row height: 200 width:parent.width spacing: 0 }
是等效的,前者就用了锚(anchors) 布局
只有在Layout相关的空间中才能使用 Layout.fillWidth: true 相关的属性。
所以 RowLayout 可以实现元素填充剩余空间,而Row 是不可以的,除非我们复制宽度是通过计算的值。
代码如下 import QtQuick 2.14 import QtQuick.Window 2.14 import QtQuick.Layouts 1.12 Window { width: 640 height: 480 visible: true color: "#f3f3f3" title: qsTr("主页面") Row { id: row height: 200 spacing: 0 anchors.left:parent.left anchors.right:parent.right Rectangle { id: rectangle width: 200 height: parent.height color: "red" } Rectangle { id: rectangle2 width: 200 height: parent.height color: "green" } Rectangle { id: rectangle3 height: parent.height width: parent.width-rectangle.width-rectangle2.width color: "blue" } } }示例2
基本的事件和按钮按压变色及点击事件 import QtQuick 2.14 import QtQuick.Window 2.14 import QtQuick.Layouts 1.12 Window { width: 640 height: 480 visible: true color: "#f3f3f3" title: qsTr("主页面") MouseArea { width: 200 height: 200 anchors.centerIn: parent Rectangle { id:myrect anchors.fill: parent color: "blue" Text { text: "点击" color: "white" font.pixelSize: 16 anchors.centerIn: parent } } onClicked: { console.log("区域点击") } onPressedChanged: { if(pressed){ myrect.color="green" }else{ myrect.color="blue" } console.log(pressed) } } Component.onCompleted: { console.log("加载完毕") } }Rectangle的事件Rectangle { width: 600 height: 400 anchors.centerIn: parent color: "lightgray" TapHandler { //点击屏幕时,修改了pressed属性,触发onPressedChanged onPressedChanged: { console.log("press ? : ", pressed) } //长按时触发onLongPressed onLongPressed: { console.log("long pressed") } } }QML 信号与槽方式1
对于 QML 中的属性如果其值发生改变, QML 自动会发生相关信号
onChanged 这种格式
举例: MouseArea { onPressedChanged: console.log("value:" , pressed) }方式2
比较适合在同一个 QML 文件内 signal (type parameter, type parameter) on
例如: signal testSignal(real x, real b) testSignal(x, b) //执行 也就是 发送信号 类似 quick 中的 emit signal() onTestSignal: console.log("xxx")// 槽 用于接收信号
举例: Item { signal clickTest(); MouseArea { onPressed: { clickTest() } } onClickTest: consloe.log("received") }方式3
适合一对多或者跨 QML 断开就使用 disconnect 就好 1 : 跟信号在同一个范围,可这么写 signal sendSignal(); MouseArea { sendSignal() } Component.onCompleted: { sendSignal.connect(send21) sendSignal.connect(send22) sendSignal.connect(send23) } function send21() { console.log("1: received signal"); } function send22() { console.log("2: received signal"); } function send23() { console.log("3: received signal"); }
2:如果与信号不在同一范围 MyTest { signal testT() id : mytest MouseArea { onPressed: { mytest.testT() } } } Component.onCompleted: { mytest.testT.connect(send21) // mytest.testT.disconnect(send21) mytest.testT.connect(send22) mytest.testT.connect(send23) } function send21() { console.log("1: received signal"); } function send22() { console.log("2: received signal"); } function send23() { console.log("3: received signal"); }
3、Connections 最主要的优势可以连接到没有定义在 QML 的东西 格式: Connections { target: 信号的来源 on: }Connections { target: mytest onTestT: { send21(); } }
ST科华2022年实现净利润约9。71亿元同比增加14。16中证网讯(记者倪铭)ST科华3月22日晚间发布年报,2022年营业收入约69。7亿元,同比增加43。58归属于上市公司股东的净利润约9。71亿元,同比增加14。16基本每股收益1。
深港协同打造科创引擎来源经济日报俯瞰河套深港科技创新合作区所在的一河两岸。(资料图片)舒糖讯息科技(深圳)有限公司首席执行官香港青年何耀威(右)介绍联动深港资源创业发展情况。李小芳摄(中经视觉)创新金
人生不过顺逆两境,强者淡然面对不因幸运而固步自封,不因厄运而一蹶不振。真正的强者,善于从顺境中找到阴影,从逆境中找到光亮,时时校准自己前进的目标。易卜生人生不过顺逆两境,顺境时不沾沾自喜,谦稳低沉才能走得更稳逆
援乌贫铀弹惊动联合国,美火速撇清,伊拉克就是这样被打残的俄媒英国宣布向乌克兰提供贫铀弹文君剑22日据央视报道,联合国秘书长副发言人哈克已经回应英国的援乌贫铀弹计划,哈克在回答央视记者提问时指出,任何使用贫铀武器的计划都会产生严重后果,联
王牌对王牌8官宣,沈腾贾玲回归,该走的没走走了的成遗憾头条创作挑战赛王牌对王牌自2016年开始已经陪伴观众走过了七年,也是如今娱乐圈仅存的为数不多的老牌综艺了。不过王牌对王牌从最开始的惊艳也渐渐变得平庸,甚至有些腻了,好在是沈腾和贾玲
1990年,徐向前去世前留下三条遗言,江主席沉思后不能全部同意在阅读此文前,诚邀您点个关注,听故事品百味人生的同时,还能获取不一样的参与感,感谢您的支持!1990年6月,开国元勋徐向前元帅因病住进了医院,他的老战友们都十分挂念他,纷纷前来探望
这些孕期关键营养素,准妈妈一定要知道!怀胎十月,一朝分娩,将近一年的怀孕时光是每个母亲最难忘的时光,也是最难熬的时光,大多在准妈妈在孕期都会格外控制自己的生活作息生活习惯,就是为了生下一个健康的宝宝。想要生下健康的宝宝
检查身体没有任何问题,可为何就是怀不上?很多备孕夫妻检查都没有问题,但备孕几个月甚至一年都无法怀孕,这就会让他们非常疑惑,究竟是怎么回事?其实这种情况有很多,只不过要针对自身情况来进行专业的治疗。那么,检查身体没问题怀不
这类维生素大部分人家都有,儿童食用要注意!防止中毒常言道是药三分毒不少人经常服用鱼肝油补充维生素可也不能敞开了吃前两天江苏省宿迁泗阳一名五岁女童不小心将鱼肝油当成糖果误吞了60多粒鱼肝油可把家长吓了一跳当天晚上七点五十分左右江苏省
亭警温度萌娃和民警的互动敬礼,超有爱!3月13日滁州公安在线抖音号后台接到这样一条网友私信于是小编就收到了这样一条超有爱的视频立刻编辑发布!视频加载中视频中萌娃看见执勤民警正在站岗手舞足蹈的跑上前给警察蜀黍们来了个大大
东汉豪强地主田庄经济的特点及其历史地位文a纵横历史观编辑a纵横历史观引言自古以来,土地是社会经济发展的重要基础。在中国封建社会中,地主阶级掌握着土地资源,形成了一套完整的经济体系,而在东汉时期,豪强地主的田庄经济成为了