QML语法

一,基本语法

1、import导入需要使用的模块。类似于C++,include。

2、一个QML文件只能有一个根元素。类似一个QML文件就是自然界的一棵树。

3、元素声明方法:类型{ }

4、元素下定义属性,属性赋值方法: name:value

5、元素可并行,也可嵌套。子元素可用通过父元素id访问它的子元素和属性。

6、一个QML文件下的元素可通过元素的id访问。

7、注释使用//或者/*     */。

二、属性

1、一个属性可用设置一个值,也可用设置与其他属性关联。id为特殊属性,不被设置其他值。

2、自定义属性需要使⽤property修饰符。方法:property    类型    名字:值

3、每个元素都可以提供⼀个属性变化信号操作。方法:on属性名Changed

4、alias关键字允许转发⼀个属性到另⼀个 作⽤域。方法:property alias :

复制代码
Text {
     // (1) id为特殊属性,不能设置其他值
     id: thisLabel
     // (2) 赋值x,y,设置位置
     x: 24; y: 16
     // (3) 设置height与width属性关联
     height: 2 * width
     // (4) 自定义属性
     property int times: 24
     // (5) property alias使用方法
     property alias anotherTimes: thisLabel.times
     // (6) 设置text属性为times关联
     text: "Greetings " + times
     // (7) 赋值组属性,也可用直接font{family: "UBuntu"; pixelSize: 24}
     font.family: "Ubuntu"
     font.pixelSize: 24
     // (8) KeyNavigation is an attached property
     KeyNavigation.tab: otherLabel
     // (9) 属性值发送改变时信号处理
     onHeightChanged: console.log('height:', height)
     // focus is need to receive key events
     focus: true
     // change color based on focus value
     color: focus?"red":"black"
}
复制代码

 三、脚本

1、QML与JavaScript是最好的配合。

复制代码
Text {
  id: label
  x: 24; y: 24
  // custom counter property for space presses
  property int spacePresses: 0
  text: "Space pressed: " + spacePresses + " times"
  // (1) handler for text changes
  onTextChanged: console.log("text changed to:", text)
  // need focus to receive key events
  focus: true
  // (2) handler with some JS
  Keys.onSpacePressed: {
    increment()
  }
  // clear the text on escape
  Keys.onEscapePressed: {
    label.text = ''
  }
  // (3) JavaScript函数
  function increment() {
    spacePresses = spacePresses + 1
  }
}
复制代码

 

注:参考文献:JRyannel,JThelin,《QT5_Cadaques》

下载链接:

链接:https://pan.baidu.com/s/1aF4e_RD9Hp1I2vMEaV2fYg
提取码:cb9h

posted @   飞说晓事  阅读(417)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示