简介  

  Component是Qt封装好的、只暴露必要接口的QML类型,可以重复利用。一个QML组件就像一个黑盒子,它通过属性、信号、函数和外部世界交互。

  一个Component既可以定义在独立的QML文件(.qml为后缀的文件)中,也可以嵌入到其他的QML文件中来定义。那么这两种方式分别适用于什么场景呢?这里有一个原则可以帮助我们去选择Component的定义方式:如果一个Component比较小且只在某个QML文件中使用,或者说一个Component从逻辑上来看属于某个QML文档,那么就可以采用嵌入的方式来定义该Component;如果这个Component有很多地方可以用到,也就是说复用率比较高,那么就可以采用定义在独立的QML文件中的方式。下面说明一下这两种实现Component方式的差别:

  • 嵌入式定义Component:

  要在一个QML文件中嵌入Component的定义,需要使用Component对象。

  定义一个Component与定义一个QML文件类似,Component只能包含一个顶层Item,而且在这个Item之外不能定义任何数据,除了id。 在顶层Item之内,则可以包含更多的子元素来协同工作,最终形成一个具有特定功能的组件。

  Component通常用来给一个View提供图形化组件,比如ListVIew::delegate属性就需要一个Component来指定如何显示列表的每一个项,又比如ButtonStyle::background属性也需要一个Component来指定如何绘制Button的背景。  

  Component不是Item的派生类,而是从QQmlComponent继承而来的,虽然它通过自己的顶层Item为其他的view提供可视化组件,但它本身是不可见元素。你可以这么理解:你定义的组件是一个新的类型,他必须被实例化以后才能显示。而要实例化一个嵌入在QML文件中定义的Component,则可以通过Loader。

  • 在单独文件中定义Component:

  很多时候我们把一个Component单独定义在一个QML文件中,比如Qt Qucik提供的BusyIndicator空间,其实就是在BusyIndicator中定义一个组件(BusyIndicator.qml):

Control {
id: indicator /*! \qmlproperty bool BusyIndicator::running This property holds whether the busy indicator is currently indicating
activity. \note The indicator is only visible when this property is set to \c true. The default value is \c true.
*/
property bool running: true Accessible.role: Accessible.Indicator
Accessible.name: "busy" style: Settings.styleComponent(Settings.style, "BusyIndicatorStyle.qml", indicator)
}

  可以看到BusyIndicator的代码非常简单,只是给Control元素(Qt Quick定义的私有元素,用作其他控件的基类,如ComboBox、BusyIndicator等)增加了一个属性,设置了几个值而已。

  BusyIndicator.qml文件中的顶层Item是Control,而我们使用时却是以BusyIndicator为组件名(类名)。这是定义Component的一个约定:组件名必须和QML文件名一致,且组件名的首字母必须是大写的。Qt Quick提供的多数基本元素和特性,都可以在定义组件时使用 。

  例子:在一个单独的QMl文件中定义颜色选择组件ColorPicker,对应QML文件为ColorPicker.qml,可以在其他的QMl文件中使用Cololr{...}来创建ColorPicker的实例。

import QtQuick 2.6

Rectangle {
id: colorPicker
width: 50
height: 30
signal colorPicked(color clr); function configureBorder() {
colorPicker.border.width = colorPicker.focus ? 2 : 0;
colorPicker.border.color = colorPicker.focus ? "#90D750" : "#808080";
} MouseArea {
anchors.fill: parent
onClicked: {
colorPicker.colorPicked(colorPicker.color);
mouse.accepted = true;
colorPicker.focus = true;
}
} Keys.onReturnPressed: { // 对应Enter键
console.log("ColorPicker:onReturnPressed");
colorPicker.colorPicked(colorPicker.color);
event.accepted = true;
} Keys.onSpacePressed: { // 对应Space键
console.log("ColorPicker:onSpacePressed");
colorPicker.colorPicked(colorPicker.color);
event.accepted = true;
} onFocusChanged: {
console.log("ColorPicker:onFocusChanged");
configureBorder();
} Component.onCompleted: {
console.log("ColorPicker:onCompleted");
configureBorder();
}
}

  在单独文件中定义Component,与嵌入式定义有明显的不同:Component对象不见了,是因为在单独文件中定义组件,不需要Component对象,只有在其他QML文件中嵌入式定义组件时才需要Component对象。

  main.qml内容:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4 Window {
visible: true
width: 320
height: 240
title: qsTr("Component") Rectangle {
width: parent.width
height: parent.height
color: "#EEEEEE" Text {
id: coloredText
anchors.centerIn: parent
anchors.top: parent.top
anchors.topMargin: 4
text: "ColorPicker"
font.pixelSize: 32
} function setTextColor(clr) {
coloredText.color = clr;
} ColorPicker {
id: redColor
color: "red"
focus: true
width: parent.width / 3 - 4
anchors.left: parent.left
anchors.leftMargin: 4
anchors.bottom: parent.bottom
anchors.bottomMargin: 4 KeyNavigation.right: blueColor
KeyNavigation.tab: blueColor
onColorPicked: {
coloredText.color = clr;
}
} ColorPicker {
id: blueColor
color: "blue"
width: parent.width / 3 - 4
anchors.left: redColor.right
anchors.leftMargin: 4
anchors.bottom: parent.bottom
anchors.bottomMargin: 4 KeyNavigation.left: redColor
KeyNavigation.right: pinkColor
KeyNavigation.tab: pinkColor
} ColorPicker {
id: pinkColor
color: "pink"
width: parent.width / 3 - 8
anchors.left: blueColor.right
anchors.leftMargin: 4
anchors.bottom: parent.bottom
anchors.bottomMargin: 4 KeyNavigation.left: blueColor
KeyNavigation.tab: redColor
} Component.onCompleted: {
blueColor.colorPicked.connect(setTextColor);
pinkColor.colorPicked.connect(setTextColor);
}
}
}

《Qt Quick核心编程》

Qt QML Component 学习笔记的更多相关文章

  1. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  2. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  3. 【Qt官方例程学习笔记】Application Example(构成界面/QAction/退出时询问保存/用户偏好载入和保存/文本文件的载入和保存/QCommandLineParser解析运行参数)

    The Application example shows how to implement a standard GUI application with menus, toolbars, and ...

  4. 【Qt官方例程学习笔记】Raster Window Example(画笔的平移/旋转/缩放应用)

    这个例子显示了如何使用QPainter渲染一个简单的QWindow. 值得学习的内容 <QtGui>头文件 #include <QtGui>就可以使用Qt GUI模块中的所有类 ...

  5. 【Qt官方例程学习笔记】Analog Clock Window Example (画笔的平移/旋转/缩放应用)

    这个例子演示了如何使用QPainter的转换和缩放特性来简化绘图. 值得学习的: 定时器事件ID检查: 在定时器事件中检查定时器id是比较好的实践. QPainter抗锯齿: We call QPai ...

  6. (转)Qt Model/View 学习笔记 (四)——创建新的Models

    创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...

  7. (转)Qt Model/View 学习笔记 (三)——Model类

    Model类 基本概念 在model/view构架中,model为view和delegates使用数据提供了标准接口.在Qt中,标准接口QAbstractItemModel类中被定义.不管数据在底层以 ...

  8. (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例

    Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...

  9. (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介

    Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...

随机推荐

  1. oracle 数据库手动备份和恢复

    一.备份命令: 1.cmd  : exp 2.cmd  :用户名/密码@ip地址/数据库名  如:     yyj/yyj@172.12.5.5/orcl    要导出的数据库 3.回车:输入要输出的 ...

  2. leetcode 1214 Two Sum BSTs

    function rob(a, b, target) { var hash = {} var stack = [a] while (queue.length) { var node = stack.p ...

  3. 《软件测试52讲》读书笔记 —— API测试怎么做

    前言 文章中还介绍了测试工具,比如cURL.postman,单API如何测试:但这些都是偏基础的东西,且网上教程各式各样,就不再赘述了:这里主要讲的就是关于复杂场景的API测试要如何应对 API测试的 ...

  4. 基于pyqt5的图片素材批量处理工具

    功能 分辨率的批量转换,文件夹递归查找 像素偏移量批量调整,文件夹单层查找 画布的大小的批量进行调整,不进行缩放,文件夹单层查找 界面 通过PyUIC生成的代码 # -*- coding: utf-8 ...

  5. Web API入参,响应规范

    入参绑定 入参应该定义成实体,而不是多个参数,方便扩展.[FromBody]和[FromUrl]特性也最好加上. public ActionResult<Pet> Create([From ...

  6. Mybaits(11)延迟加载

    一.概述 1.概念 就是在需要用到数据时才去进行加载,不需要用的数据就不加载数据.延迟加载也称为懒加载. 2.优缺点 优点:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要 ...

  7. PLSQL Developer12注册码

    product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number:226959 password: xs374ca 绝对靠谱

  8. 10day 系统的selinux服务程序优化

    selinux服务对root用户权限进行控制 很多企业中:selinux服务默认关闭 centos6==centos7 临时关闭: 检查确认: getenforce --- 确认selinux服务是否 ...

  9. 题解【洛谷P2863】 [USACO06JAN]牛的舞会The Cow Prom

    题面 题解 \(Tarjan\)板子题. 统计出大小大于\(1\)的强连通分量数量输出即可. 代码 #include <iostream> #include <cstdio> ...

  10. 2019牛客多校第三场B Crazy Binary String 思维

    Crazy Binary String 思维 题意 给出01串,给出定义:一个串里面0和1的个数相同,求 满足定义的最长子序列和子串 分析 子序列好求,就是0 1个数,字串需要思考一下.实在没有思路可 ...