当前位置: 首页 > news >正文

QML组件

一个QML文件定义了一个独立的、顶级的QML组件。

一个QML组件就是一个模板,被QML运行环境解释来创建一个带有一些预定义行为的对象。

一个独立的QML组件可以运行多次来禅城多个对象,每个对象都可以称为该组件的实例。

例子:

在项目中添加一个Mywidget.qml文件

添加以下代码:

import QtQuick 2.9Rectangle{width: 100height:100Text{anchors.fill:parentfont.bold: truefont.pixelSize: 20text: "矩形"}
}

在main.qml中添加以下代码:

Column{spacing: 20//创建了3个Mywidget组件Mywidget{id:widget1}Mywidget{id:widget2}Mywidget{id:widget3}}

任意的QML代码片段都可以称为一个组件,只需要将它放入一个 Xxxx.qml文件中,但必须以大写字母开头。

内联组件:

 内联组件使用Component元素声明,拥有常规顶级组件的所有特性。

内联组件并不会自动呈现和显示 :

Rectangle{width: 300height: 300color: "lightblue"Component{id:component1Rectangle{width: 100height: 100color: "green"}}}

 使用加载器加载:

Rectangle{width: 300height: 300color: "lightblue"Component{id:component1Rectangle{width: 100height: 100color: "green"}}}Loader{sourceComponent: component1}

 也可以为视图提供图形组件:

例如:使用ListView 中的delegate 来获取Component

Rectangle{width: 300height: 300color: "lightblue"Component{id:component1Rectangle{width: 100height: 100color: "green"}}ListView{anchors.fill:parentmodel: contactModel//显示的模型delegate:component1//代理}}

相当于:

Rectangle{width: 300height: 300color: "lightblue"ListView{anchors.fill:parentmodel: contactModel//显示的模型delegate:Rectangle{width: 100height: 100color: "green"}}}

在组件中添加属性、函数和信号

添加属性:

属性:可以在外部进行访问来修改一个对象,如Rectangle的width属性

  1. 一个属性就是一个QML组件中的一个值,可以被其他对象读取和修改
  2. 属性可以用于属性绑定,可以和其他属性同步修改

定义一个新属性的语法:

[default] property<type><name>[:defaultValue]
Rectangle{id:rect1property string image1: "qrc:/pix/ggg.png" //创建一个新属性width:200height: 200color: "lightBlue"Image {anchors.fill:parentsource: rect1.image1//使用新属性}}

 QML常见的属性类型:

QML类型默认值C++(Qt)
int0int
boolfalsebool
double0.0double
real0.0double
string""QString
url""QUrl
color#000000(黑色)QColor
date未定义QDateTime
variant未定义QVariant

默认属性:

  1. 可以在声明属性时选default,可以使该属性成为默认属性
  2. 允许其他对象来指定默认属性值来作为子元素

Item元素默认属性为children属性:

Item{Rectangle{}Rectangle{}}

 如果children属性不是Item的默认属性的话,需要使用children[ ]

Item{children: [Rectangle{},Rectangle{}]}

属性的别名:

属性别名是更高级的属性声明形式,属性别名不需要分配新的存储空间,可以通过别名来对属性进行操作。

别名的格式:类型被省略,但一定要加上alias关键词。

[dafault] property alias <name> :<alias reference>alias关键字允许我们转发一个属性或者转发一个属性对象自身到另一个作用域

使用的注意事项: 

  1. 只有在指定它们的部件创建完时,别名才可用,在组件本身创建时不能直接使用别名属性
  2. 别名引用不能使用在同一个部件中声明的另一个别名属性
  3. 一个别名属性可以和现有的属性使用相同的名称
//只有在指定它们的部件创建完时,别名才可用,在组件本身创建时不能直接使用别名属性property alias label:text1.text
alis:"GGGGG"//使用时text1.text还没定义//别名引用不能使用在同一个部件中声明的另一个别名属性id:root
property alias buttonText:textItem.text
property alias buttonText2:root.buttonText//一个别名属性可以和现有的属性使用相同的名称
Rectangle{property alias color:childRect.colorcolor:"red"Rectangle{id:childRect}
}
在这里color操作的都是childRect的color

例子:(添加一个Mywidget.qml文件)

import QtQuick 2.9
Rectangle{property alias label:text1 //起别名,使得另一个qml文件也可以访问width: 100height:100Text{id:text1anchors.fill:parentfont.bold: truefont.pixelSize: 20text: "矩形"}
}

在main.qml中创建一个Mywidget对象:

Rectangle{Mywidget{id:mywidget1label.text:"ppppppp"//通过别名访问Text中的内容}   }

添加函数

QML中可以使用JavaScript代码的函数,这些函数可以在内部调用,也可以被其他对象调用。

函数格式:

function <name>([<parameter name][,...])
{<body>}
  1. 声明可以出现在任何地方,但一般出现在顶部,方便查看
  2. 函数的参数类型为variant,所以不需要函数参数类型

无参函数 :

function fun1(){console.log("无参函数")}

有参函数:

function fun1(s1,s2){console.log("s1+s2为",s1+s2)}

有返回值的函数:

function fun1(s1,s2){return Math.max(s1,s2)//返回这两个的最大值}

常用的数学函数:格式:Math.xxx

添加信号

当一个事件发生时可以发射信号。信号的声明可以放到任意位置,但一般放置在开头。

信号格式为: 

signal <name>[([<type> <parameter name>[,...]])]

信号没有参数的话可以省略()

例子:

Item{signal clicksignal hovered()signal performAction(string action,variant actionArgument)}

连接的创建:on<SignalName>来命名,信号名的第一个字母要大写

//上面对应的槽函数
onClicked
onHovered
onPerformAction

 触发信号:直接调用函数即可

例子:创建一个Mywidget.qml

import QtQuick 2.9Rectangle{id:rect1signal buttonClicked(int X,int Y)width: 100height:100MouseArea{anchors.fill:parentonClicked: {rect1.buttonClicked(mouse.x,mouse.y)//鼠标点击后,触发信号}}
}

 main.qml中添加

Mywidget{width: 100height: 100//槽函数onButtonClicked: { console.log("鼠标位置为:"+X+","+Y);}}

将信号关联到其他函数和信号上

使用connect()函数,可以将一个信号关联到一个函数或者其他信号上

 创建一个Mywidget.qml:

import QtQuick 2.9Rectangle{id:rect1signal buttonClicked(int X,int Y)width: 100height:100MouseArea{anchors.fill:parentonClicked: {rect1.buttonClicked(mouse.x,mouse.y)//鼠标点击后,触发信号}}
}

main.qml中添加:

Item{anchors.fill:parentid:item1function text1(){  //创建一个函数console.log("新的函数运行")}Mywidget{width: 100height: 100Component.onCompleted: buttonClicked.connect(item1.text1)//将信号关联到text1上}}

使用connections连接

on<SignalName>无法使用的情况:

  1.  需要对同一信号进行多次连接
  2. 在信号发送方范围之外创建连接
  3. 连接到QML未定义的目标

例子:Mywidget.qml

import QtQuick 2.9Rectangle{id:rect1signal buttonClicked(int X,int Y)width: 100height:100MouseArea{anchors.fill:parentonClicked: {rect1.buttonClicked(mouse.x,mouse.y)//鼠标点击后,触发信号}}
}

main.qml

Item{anchors.fill:parentid:item1function text1(){  //创建一个函数console.log("新的函数运行")}Mywidget{id:my1width: 100height: 100}Connections{target: my1//关联到该控件onButtonClicked:{item1.text1()}}}

 

http://www.lryc.cn/news/28368.html

相关文章:

  • canvas 学习指南
  • 【华为OD机试2023】开心消消乐 C++
  • 学历?能力?
  • 使用ECharts打造一个数据可视化面板
  • 【论文简述】PVSNet: Pixelwise Visibility-Aware Multi-ViewStereo Network(arxiv 2020)
  • CSS隐藏元素的几种方式以及display、visibility、opacity的区别
  • 【Java|golang】1487. 保证文件名唯一---golang中string方法的坑
  • flstudio21水果language选项中文设置方法教程
  • Ubuntu中安装StaMPS
  • Spring Security 实现自定义登录和认证(1)
  • Linux 进程:辨析wait与waitpid
  • 移除元素(每日一题)
  • 打印名片-课后程序(Python程序开发案例教程-黑马程序员编著-第一章-课后作业)
  • 为啥预编译SQL能够防止SQL注入
  • IGKBoard(imx6ull)-SPI接口编程-回环测试
  • Python基础学习10——类
  • 项目实战典型案例14——代码结构混乱 逻辑边界不清晰 页面美观设计不足
  • SpringBoot 读取自定义Properties参数
  • 机器学习100天(三十七):037 朴素贝叶斯-挑个好西瓜!
  • c#遍历窗口,根据标题获取handle并显示窗口
  • MyBatis高频面试专题
  • 曹云金郭德纲关系迎曙光,新剧《猎黑行动》被德云社弟子齐点赞
  • 如何在 OpenEuler 系统中安装 Docker
  • MySQL日志管理
  • 进 制
  • pycharm关联github、新建以及更新仓
  • java基础知识之小碎片(自问自答版本)---嘻嘻,春招加油呀~
  • 蚁群算法c++
  • 北大青鸟天府校区IT学习大揭秘
  • 04 Linux errno.h错误码中文注释