当前位置: 首页 > 编程日记 > 正文

qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516201

qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

本学章节笔记主要详解Item元素(上半场主要涉及anchors锚),因为所有可视化的界面元素都继承于Item,熟悉Item后,不同的继承子类,有其定制的属性(从几个到几十个不等)。

《Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)》:
http://blog.csdn.net/qq21497936/article/details/78486552
    《qml学习笔记(一):界面元素初探》:
http://blog.csdn.net/qq21497936/article/details/78498575

《qml学习笔记(三):可视化元素基类Item详解(下半场)》
http://blog.csdn.net/qq21497936/article/details/78522816

基类Item介绍

基类Item是所有可视化子类的父类,它不是可见的,但是它定义了所有通用元素通用的属性,比如x、y、width、height、anchoring和handingsuppourt。

几个Item的使用示例

Image示例

[css] view plain copy
  1. Item{
  2. Rectangle{
  3. width:1000;  
  4. height:1000;  
  5. color:"black";  
  6. Image { // Image默认的背景是透明
  7. source:"1.png"// 相对于.qml的路径  
  8. }
  9. Image{
  10. x:80  
  11. y:200  
  12. width:100  
  13. height:100  
  14. source:"1.png"  
  15. }
  16. Image{
  17. x:190  
  18. y:400  
  19. width:100  
  20. height:100  
  21. fillMode:Image.Tile
  22. source:"1.png"  
  23. }
  24. }
  25. }


    效果如下图:

捕捉键盘

[css] view plain copy
  1. Item{
  2. focus:true
  3. Keys.onPressed:{
  4. if(event.key==Qt.Key_Left){
  5. console.log("moveleft");  
  6. event.accepted=true;
  7. }
  8. }
  9. Keys.onReturnPressed:
  10. console.log("Pressedreturn");  
  11. }

输入处理

[css] view plain copy
  1. Rectangle{
  2. width:100;  
  3. height:100  
  4. FocusScope{
  5. id:focusScope
  6. focus:true
  7. TextInput{
  8. id:input
  9. focus:true
  10. }
  11. }
  12. }

效果如图

属性详解

activeFocus : bool [可读写][指示焦点:窗口是否获取焦点]

此属性指示元素是否具有活动焦点。如果指示是真的,这个对象是目前接收键盘输入,或是一个FocusScope为父对象的对象,目前接收键盘输入。

通常,此属性是通过设置焦点在其子元素(继承于Item)和其外围FocusScope对象获得。在下面的例子中,TextInput和FocusScope对象会有活跃的热点,而根矩形对象将不会。

activeFocusOnTab : bool [可读写][设置item是否可被tab选中,默认为false]

anchors:一组属性,提供了以元素相互关系为基准的定位方式,主要包括以下的:

anchors.top : AnchorLine [可读写][顶部边界]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:100;  
  5. y:200;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.top: pic.bottom; // 对象的顶部是与pic的底部高度相同  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  14. }
  15. }

anchors.bottom : AnchorLine [可读写][底部边界]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:100;  
  5. y:200;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.bottom: pic.bottom; // 对象的顶部是与pic的底部高度相同  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  14. }
  15. }

anchors.left : AnchorLine [可读写][左边界]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:100;  
  5. y:10;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.left: pic.right; // 对象的顶部是与pic的底部高度相同  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  14. }
  15. }

anchors.right : AnchorLine [可读写][右边界]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:100;  
  5. y:10;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.right: pic.right; // 对象的顶部是与pic的底部高度相同  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位为像素,依赖于设备  
  14. }
  15. }

anchors.horizontalCenter : AnchorLine [可读写][水平中心]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. source: "./1.png";  
  5. }
  6. Text {
  7. id: label
  8. // 对象的水平中心 以 pic的水平中心 为中心
  9. anchors.horizontalCenter: pic.horizontalCenter;
  10. text: "hello world";  
  11. color: "white";  
  12. font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  13. }
  14. }

anchors.verticalCenter : AnchorLine [可读写][垂直中心]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:100;  
  5. y:10;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.verticalCenter: pic.bottom; // 对象的顶部是与pic的底部高度相同  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  14. }
  15. }

anchors.baseline : AnchorLine AnchorLine [可读写][baseline是指的文本所在的线,如果item没有文字的话baseline就和top的位置是相同的]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:40;  
  5. y:40;  
  6. source: "./1.png";  
  7. }
  8. Text {
  9. id: label;
  10. anchors.baseline: pic.top;  
  11. text: "hello world";  
  12. color: "black";  
  13. font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备  
  14. }
  15. }

anchors.fill : Item [可读写][用本对象填充指向的对象元素]

[css] view plain copy
  1. Item{
  2. Image{
  3. id:pic;
  4. x:40;  
  5. y:40;  
  6. source:"./1.png";  
  7. }
  8. Rectangle{
  9. id:label;
  10. anchors.fill:pic; // 此时设置width和height,测试无效,直接填满pic
  11. color:"black";  
  12. }
  13. }

anchors.centerIn : Item [可读写][用本对象的中心对准指向对象的中心,开始辐射出去,区域可大于设置指向的对象]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:40;  
  5. y:40;  
  6. source: "./1.png";  
  7. }
  8. Rectangle {
  9. id: label;
  10. width: 60;  
  11. height: 60;  
  12. anchors.centerIn: pic; // 以pic的中心为该对象中心进行辐射(区域可大于pic)
  13. color: "black";  
  14. }
  15. }

anchors.margins : real [可读写][设置所有(top,bottom,left,right)边框的宽度]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. x:40;  
  5. y:40;  
  6. source: "./1.png";  
  7. }
  8. Rectangle {
  9. id: label;
  10. width: 60;  
  11. height: 60;  
  12. color: "black";  
  13. anchors.margins: 10;  
  14. anchors.left: pic.right;  
  15. }
  16. Rectangle {
  17. id: label2;  
  18. width: 60;  
  19. height: 60;  
  20. color: "black";  
  21. anchors.margins: 10;  
  22. anchors.top: pic.bottom;  
  23. }
  24. }

[css] view plain copy
  1. Item {
  2. Rectangle {
  3. id: label;
  4. width: 60;  
  5. height: 60;  
  6. color: "red";  
  7. anchors.margins: 50;  
  8. }
  9. Rectangle {
  10. id: label2;  
  11. width: 60;  
  12. height: 60;  
  13. color: "black";  
  14. anchors.margins: 50; // 只对本对象设置anchors边框有效  
  15. anchors.top: label.bottom;  
  16. }
  17. Rectangle {
  18. id: labe3;  
  19. width: 60;  
  20. height: 60;  
  21. color: "red";  
  22. anchors.margins: 50; // 只对本对象设置anchors边框有效  
  23. anchors.top: labe2.bottom;  
  24. }
  25. }

anchors.topMargin : real [可读写][设置top边框的宽度,参照margins]

anchors.bottomMargin : real [可读写][设置bottom边框的宽度,参照margins]

anchors.leftMargin : real [可读写][设置left边框的宽度,参照margins]

anchors.rightMargin : real [可读写][设置right边框的宽度,参照margins]

anchors.horizontalCenterOffset : real [可读写][设置水平中心偏移量]

[css] view plain copy
  1. Item {
  2. Image {
  3. id: pic;
  4. source: "./1.png";  
  5. }
  6. Rectangle {
  7. width: 30;  
  8. height: 30;  
  9. id: rect;
  10. color: "black";  
  11. // 对象的水平中心 以 pic的水平中心 为中心
  12. anchors.horizontalCenter: pic.horizontalCenter;
  13. // 注意:horizomtalCenterOffset针对于horizontalCenter
  14. anchors.horizontalCenterOffset: 50;  
  15. }
  16. }

anchors.verticalCenterOffset : real [可读写][参照设horizontalCenter,与其类似]

anchors.baselineOffset : real[可读写][参照设horizontalCenter,与其类似]

anchors.alignWhenCentered : bool [可读写][指定不使用半个像素绘制图形,当需要居中一个elements,宽度或者高度是基数,不使用半个像素绘制,对此处理解有疑问]

下章节

《qml学习笔记(三):可视化元素基类Item详解(下半场)》:http://blog.csdn.net/qq21497936/article/details/78522816

原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516201

转载于:https://www.cnblogs.com/senior-engineer/p/7986866.html

相关文章:

使用PowerShell登陆多台Windows,测试DCAgent方法

目标: 需要1台PC用域账户远程登陆10台PC,每台登陆后的PC执行发送敏感数据的操作后,再logoff。 在DCAgent服务器上,查看这10个用户每次登陆时,DCAgent是否能获取到登陆信息(IP:User) …

优雅地分离tableview回调

你是否遇到过这样的需求,在tableview中显示一列数据,点击某一个cell时,在此cell下显示相应的附加信息。如下图:你是不是觉得需求很容易实现,只要使用tableview的insertRowsAtIndexPaths:withRowAnimation:插入一个附加cell就可以了&#xff0…

next.js_Next.js手册

next.jsI wrote this tutorial to help you quickly learn Next.js and get familiar with how it works.我编写本教程是为了帮助您快速学习Next.js并熟悉其工作方式。 Its ideal for you if you have zero to little knowledge of Next.js, you have used React in the past,…

Redux 入门教程(一):基本用法

一年半前,我写了《React 入门实例教程》,介绍了 React 的基本用法。 React 只是 DOM 的一个抽象层,并不是 Web 应用的完整解决方案。有两个方面,它没涉及。代码结构组件之间的通信对于大型的复杂应用来说,这两方面恰恰…

Elasticsearch——Rest API中的常用用法

本篇翻译的是Elasticsearch官方文档中的一些技巧,是使用Elasticsearch必不可少的必备知识,并且适用于所有的Rest Api。 返回数据格式化 当在Rest请求后面添加?pretty时,结果会以Json格式化的方式显示。另外,如果添加?formatyaml…

Python几种主流框架

从GitHub中整理出的15个最受欢迎的Python开源框架。这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等。 Django: Python Web应用开发框架Django 应该是最出名的Python框架,GAE甚至Erlang都有框…

Git Fetch vs Pull:Git Fetch和Git Pull命令之间有什么区别?

Git pull and fetch are two commands that are regularly used by Git users. Let’s see the difference between both commands.Git pull和fetch是Git用户经常使用的两个命令。 让我们看看两个命令之间的区别。 For the sake of context, it’s worth remembering that we’…

Redux 入门教程(二):中间件与异步操作

上一篇文章,我介绍了 Redux 的基本做法:用户发出 Action,Reducer 函数算出新的 State,View 重新渲染。但是,一个关键问题没有解决:异步操作怎么办?Action 发出以后,Reducer 立即算出…

day09_读写分离_组件介绍

mysql中间件研究(Mysql-prxoy,Atlas,阿米巴,cobar,TDDL)mysql-proxyMySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转…

Idea其他设置

一、生成javadoc Tools->Gerenate JavaDoc 1. 选择是整个项目还是模块还是单个文件 2. 文档输出路径 3. Locale 选择地区,这个决定了文档的语言,中文就是zh_CN 4. 传入JavaDoc的参数,一般这样写 -encoding UTF-8 -charset UTF-8 -windowti…

freecodecamp_常见技术支持问题– freeCodeCamp常见问题解答

freecodecamp问题:我刚刚登录我的帐户,但看不到过去的任何进展。 (Question: I just signed into my account and I dont see any of my past progress.) Answer: You have created a duplicate account. Sign out of your account and try signing in u…

Redux 入门教程(三):React-Redux 的用法

前两篇教程介绍了 Redux 的基本用法和异步操作,今天是最后一部分,介绍如何在 React 项目中使用 Redux。 为了方便使用,Redux 的作者封装了一个 React 专用的库 React-Redux,本文主要介绍它。 这个库是可以选用的。实际项目中&…

SDN第二次上机作业

1、安装floodlight 2、生成拓扑并连接控制器floodlight,利用控制器floodlight查看图形拓扑 3、利用字符界面下发流表,使得‘h1’和‘h2’ ping 不通 4、利用字符界面下发流表,通过测试‘h1’和‘h3’的联通性,来验证openflow的har…

[KIWI syslog]Install document

安全日志的标准是rfc5425 它介绍SSL-tunnel日志标准和相关要求的标准定义。此外,IANA分配TCP端口6514作为一个标准的端口安全日志。 安装说明: 1.安装日志服务器 官方下载地址:http://www.kiwisyslog.com/products/. 安装步骤: 转…

机器学习数据拆分_解释了关键的机器学习概念-数据集拆分和随机森林

机器学习数据拆分数据集分割 (Dataset Splitting) Splitting up into Training, Cross Validation, and Test sets are common best practices. This allows you to tune various parameters of the algorithm without making judgements that specifically conform to trainin…

Oracle 导表异常处理方案 (解决空表导出出错问题)

Select alter table ||table_name|| allocate extent; from user_tables where num_rows0 or num_rows is null 然后执行查询语句 再导出数据 一个语句搞定: declare stmt varchar2(200); begin for tb in (select table_name from user_tables where seg…

10个你必须知道的ios框架

你好,iOS 开发者们!我的名字叫 Pawe?,我是一个独立 iOS 开发者,并且是 Enter Universe 的作者。 接近两年前我发布了iOS开源库,让你的开发坐上火箭吧。这是我在这里最棒的文章了(根据 Medium 用户的反馈来…

生成N个不相等的随机数

近期项目中须要生成N个不相等的随机数。实现的时候。赶工期&#xff0c;又有项目中N非常小(0-100)直接谢了一个最直观的方法: public static List<Integer> randomSet(int num,int threshold){Random random new Random();if(num > threshold) return null;Set<In…

kafka streams_如何使用Kafka Streams实施更改数据捕获

kafka streamsChange Data Capture (CDC) involves observing the changes happening in a database and making them available in a form that can be exploited by other systems. 更改数据捕获 (CDC)涉及观察数据库中发生的更改&#xff0c;并将其以可被其他系统利用的形式…

iOS超全开源框架、项目和学习资料汇总(1)UI篇

上下拉刷新控件**1. ** MJRefresh --仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能。可以自定义上下拉刷新的文字说明。&#xff08;推荐&#xff09;**2. ** SVPullToRefresh --下拉刷新控件4500star&#xff0c;值得信赖**3. ** CBStoreHo…

day16 递归函数

一、递归 函数 为什么要有函数&#xff0c;提高代码的可读性&#xff0c;避免重复的代码&#xff0c;提高代码的复用性 在函数中能用return的不要print 1、递归的最大深度997 def foo(n):print(n)n1foo(n) foo(1) 递归的最大深度2、修改递归的最大深度 由此我们可以看出&#x…

设计模式之笔记--抽象工厂模式(Abstract Factory)

抽象工厂模式&#xff08;Abstract Factory&#xff09; 定义 抽象工厂模式&#xff08;Abstract Factory&#xff09;&#xff0c;提供一个创建一系列相关或相互依赖对象的接口&#xff0c;而无需指定它们具体的类。 类图 描述 多个抽象产品类&#xff0c;每个抽象产品类可以派…

用户体验改善案例_如何检测用户的设备,以便改善他们的用户体验

用户体验改善案例A few months ago I watched a great talk from the Chrome Dev Summit about performance in slow devices.几个月前&#xff0c;我观看了Chrome开发者峰会上有关慢速设备性能的精彩演讲。 It blew my mind all the work done by Facebook in identifying de…

【如何快速的开发一个完整的iOS直播app】(采集篇)

前言在看这篇之前&#xff0c;如果您还不了解直播原理&#xff0c;请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇)开发一款直播app&#xff0c;首先需要采集主播的视频和音频&#xff0c;然后传入流媒体服务器&#xff0c;本篇主要讲解如何采集主播的视频和音频&am…

easyui 报表合并单元格

前段时间工作中碰到有需求&#xff0c;要求数据按下图所示格式来显示&#xff0c;当时在园子里看到了一篇文章&#xff08;时间久了&#xff0c;想不起是哪一篇&#xff09;&#xff0c;研究了后做出了如下的DEMO&#xff0c;在此当作学习笔记&#xff0c;简单记录一下。 首先是…

HDU2594 KMP next数组的应用

这道题就是给你两个串s1, s2让你求出s1 s2的最长相同前缀和后缀&#xff0c; 我们直接将s1 s2连接到一起然后处理一下next数组即可&#xff0c; 注意答案应该是min(len(s1), len(s2) , next[len]), 代码如下&#xff1a; #include <cstdio> #include <cstring> #in…

c语言中浮点数和整数转换_C中的数据类型-整数,浮点数和空隙说明

c语言中浮点数和整数转换C中的数据类型 (Data Types in C) There are several different ways to store data in C, and they are all unique from each other. The types of data that information can be stored as are called data types. C is much less forgiving about d…

【如何快速的开发一个完整的iOS直播app】(美颜篇)

前言在看这篇之前&#xff0c;如果您还不了解直播原理&#xff0c;请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇)开发一款直播app&#xff0c;美颜功能是很重要的&#xff0c;如果没有美颜功能&#xff0c;可能分分钟钟掉粉千万&#xff0c;本篇主要讲解直播中美颜…

Linux内核分析——第五章 系统调用

第五章 系统调用 5.1 与内核通信 1、系统调用在用户空间进程和硬件设备之间添加了一个中间层&#xff0c;该层主要作用有三个&#xff1a; &#xff08;1&#xff09;为用户空间提供了一种硬件的抽象接口 &#xff08;2&#xff09;系统调用保证了系统的稳定和安全 &#xff08…

BZOJ 3110

http://www.lydsy.com/JudgeOnline/problem.php?id3110 整体二分区间修改树状数组维护 #include<cstdio> #define FOR(i,s,t) for(register int is;i<t;i) inline int max(int a,int b){return a>b?a:b;} inline int min(int a,int b){return a<b?a:b;} type…