原博主博客地址: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

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

  1. python列表和元组的应用_python学习笔记之列表(list)与元组(tuple)详解

    前言 最近重新再看python的基础知识,感觉自己还是对于这些知识很陌生,需要用的时候还是需要翻书查阅,还是先注重基础吧--我要重新把python的教程阅读一遍,把以前自己忽略的部分学习,加强练习和记 ...

  2. moviepy音视频剪辑:视频剪辑基类VideoClip详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...

  3. 【matcovnet学习笔记】objective,top1error,top5error详解

    [matcovnet学习笔记]objective,top1error,top5error详解 排名前1和前5的错误率是衡量某些解决方案成功与否的重要单位 ,要理解这三个概念,关键是要看懂下面这个多类误 ...

  4. Ext.Net学习笔记22:Ext.Net Tree 用法详解

    上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat="server"><Root> ...

  5. Java记录 -22- Java的基类Object详解

    Java的基类Object详解 Java的JDK文档要经常查阅使用,最好查看英文的文档. Oracle官方在线 Java API Specifications http://www.oracle.co ...

  6. 【Azure 架构师学习笔记】-Azure Data Factory (4)-触发器详解-事件触发器

    本文属于[Azure 架构师学习笔记]系列. 本文属于[Azure Data Factory]系列. 接上文[Azure 架构师学习笔记]-Azure Data Factory (3)-触发器详解-翻 ...

  7. 【转】Asp.net控件开发学习笔记整理篇 - WebControl基类

    最近一直在做MVC项目,对于WEBFORM 好像快忘记了.周末无聊,顺带看看他人的笔记.再次温习下. 复习大纲: 导航.页面生命周期及其它导论 一.服务器控件生命周期 二.控件开发基础 三.Asp.n ...

  8. 【C++】【学习笔记】【递归与回溯问题详解与例题】排列问题;组合问题;二维平面回溯;flood fill问题;搜索问题(八皇后);

    目录 七.递归和回溯 1.回溯 2.回溯应用 - 排列问题 2.回溯应用 - 组合问题 3.回溯应用 - 二维平面 4.回溯应用 - floodfill算法 问题 4.回溯应用 - 搜索问题 - 八皇 ...

  9. Apollo星火计划学习笔记第六讲——Apollo感知模块详解实践2感知基础

    Apollo学习笔记 零.目录 一.Apollo感知框架介绍 1.1 检测和分类 1.2 跟踪 1.3 感知模块代码结构 1.3.1 感知模块入口 二.Lidar障碍物检测.红绿灯识别 2.1 Lid ...

最新文章

  1. jQuery中的.height()、.innerHeight()和.outerHeight()
  2. 数学战神app(小学生四则运算app)进度
  3. asm冗余 oracle_oracle asm 磁盘管理什么场景该用什么样的冗余方式
  4. SpringBoot阿里巴巴Fastjson的一些常用配置
  5. 用C/C++扩展你的PHP
  6. FISCO BCOS 区块链 设置交易最晚处理区块高度
  7. php操作mysql工具类_PHP操作数据库的工具类
  8. PMP试题 | 每日一练,快速提分 9.3
  9. 汇通达网络IPO取发行区间下限定价,多家投资方将出现账面亏损
  10. openpyxl插入分页符
  11. 天线基础知识(四)接收灵敏度
  12. Racket读写JSON
  13. 筒灯可以执行CAN/ULC-S101测试吗?与BS 476-21区别大吗?
  14. 对d3d9里面的函数挂钩实现透视
  15. 手动给tabcontrol的tabPage加图标图片方法
  16. web.py中通过POST接收Json数据解析的bug
  17. 三元运算符 php_使用PHP三元运算符
  18. 如何登录锐捷设备(网关篇)
  19. 戒指的带法,终于收齐了
  20. UE4地编基础-材质蓝图篇

热门文章

  1. Python学习笔记1 Python基础
  2. TensorFlow基本计算单元:代码示例
  3. html离开页面时,js实现用户离开页面前提示是否离开此页面的方法(包括浏
  4. matlab pup,matlab利用bar函数画不同颜色直方图
  5. arm-linux-ld中的参数,arm-linux-ld指令详解
  6. x86架构手机_都是芯片,为什么电脑CPU不能用在手机里?
  7. android zxing作用,Android / ZXing不再有效
  8. cap mysql_.NetCore关于Cap(RabbitMQ)结合MySql使用出现MySql相关类冲突问题解决办法
  9. MySQL数据库实用教程考核_《MySQL数据库实用教程》郑明秋,蒙连超,赵海侠【pdf】...
  10. Iar环境c语言调用汇编函数,如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数?...