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

简介: qml开发笔记(二):可视化元素基类Item详解(上半场anchors等等)

若该文为原创文章,未经允许不得转载

原博主博客地址:https://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

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

各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究

目录

前言

基类Item介绍

Image示例

捕捉键盘

输入处理

效果

属性详解


红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中...(点击传送门)

Qt开发专栏:qml开发(点击传送门)

 

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

 

前言

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

 

基类Item介绍

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

   几个Item的使用示例    

Image示例

Item{
    Rectangle{
        width:1000;
        height:1000;
        color:"black";
        Image { // Image默认的背景是透明
        source:"1.png"// 相对于.qml的路径
        }
        Image{
            x:80
            y:200
            width:100
            height:100
            source:"1.png"
        }
        Image{
            x:190
            y:400
            width:100
            height:100
            fillMode:Image.Tile
            source:"1.png"
        }
    }
}

   效果如下图:

图片.png

捕捉键盘

Item{
    focus:true
    Keys.onPressed:{
        if(event.key==Qt.Key_Left){
            console.log("moveleft");
            event.accepted=true;
        }
    }
    Keys.onReturnPressed:
        console.log("Pressedreturn");
}

输入处理

Rectangle{
width:100;
height:100
    FocusScope{
    id:focusScope
    focus:true
    TextInput{
        id:input
        focus:true
        }
    }
}

效果

图片.png

 

属性详解

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

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

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

  • activeFocusOnTab : bool [可读写][设置item是否可被tab选中,默认为false]
  • anchors:一组属性,提供了以元素相互关系为基准的定位方式,主要包括以下的:
  • anchors.top : AnchorLine [可读写][顶部边界]
Item {
    Image {
        id: pic;
        x:100;
        y:200;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.top: pic.bottom; // 对象的顶部是与pic的底部高度相同
        text: "hello world";
        color: "black";
        font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

图片.png

Item {
    Image {
        id: pic;
        x:100;
        y:200;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.bottom: pic.bottom; // 对象的顶部是与pic的底部高度相同
        text: "hello world";
        color: "black";
        font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

图片.png

Item {
    Image {
        id: pic;
        x:100;
        y:10;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.left: pic.right; // 对象的顶部是与pic的底部高度相同
        text: "hello world";
        color: "black";
        font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

 

图片.png

Item {
    Image {
        id: pic;
        x:100;
        y:10;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.right: pic.right; // 对象的顶部是与pic的底部高度相同
        text: "hello world";
        color: "black";
        font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位为像素,依赖于设备
    }
}

图片.png

Item {
    Image {
        id: pic;
        source: "./1.png";
    }
    Text {
        id: label
        // 对象的水平中心 以 pic的水平中心 为中心
        anchors.horizontalCenter: pic.horizontalCenter;        
        text: "hello world";
        color: "white";
        font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

图片.png

Item {
    Image {
        id: pic;
        x:100;
        y:10;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.verticalCenter: pic.bottom; // 对象的顶部是与pic的底部高度相同
        text: "hello world";
        color: "black";
        font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

图片.png

  • anchors.baseline : AnchorLine AnchorLine [可读写][baseline是指的文本所在的线,如果item没有文字的话baseline就和top的位置是相同的]
Item {
    Image {
        id: pic;
        x:40;
        y:40;
        source: "./1.png";
    }
    Text {
        id: label;
        anchors.baseline: pic.top;
        text: "hello world";
        color: "black";
        font.pointSize: 14; //大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
    }
}

图片.png

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

 

Item{
    Image{
        id:pic;
        x:40;
        y:40;
        source:"./1.png";
    }
    Rectangle{
        id:label;
        anchors.fill:pic; // 此时设置width和height,测试无效,直接填满pic
        color:"black";
    }
}

图片.png

  • anchors.centerIn : Item [可读写][用本对象的中心对准指向对象的中心,开始辐射出去,区域可大于设置指向的对象]
Item {
    Image {
        id: pic;
        x:40;
        y:40;
        source: "./1.png";
    }
    Rectangle {
        id: label;
        width: 60;
        height: 60;
        anchors.centerIn: pic; // 以pic的中心为该对象中心进行辐射(区域可大于pic)
        color: "black";
    }
}

图片.png

  • anchors.margins : real [可读写][设置所有(topbottomleftright)边框的宽度]
Item {
    Image {
        id: pic;
        x:40;
        y:40;
        source: "./1.png";
    }
    Rectangle {
        id: label;
        width: 60;
        height: 60;
        color: "black";
        anchors.margins: 10;
        anchors.left: pic.right;
    }
    Rectangle {
        id: label2;
        width: 60;
        height: 60;
        color: "black";
        anchors.margins: 10;
        anchors.top: pic.bottom;
    }
}

图片.png

Item {
    Rectangle {
        id: label;
        width: 60;
        height: 60;
        color: "red";
        anchors.margins: 50;
    }
    Rectangle {
        id: label2;
        width: 60;
        height: 60;
        color: "black";
        anchors.margins: 50; // 只对本对象设置anchors边框有效
        anchors.top: label.bottom;
    }
    Rectangle {
        id: labe3;
        width: 60;
        height: 60;
        color: "red";
        anchors.margins: 50; // 只对本对象设置anchors边框有效
        anchors.top: labe2.bottom;
    }
}

图片.png

Item {
    Image {
        id: pic;
        source: "./1.png";
    }
    Rectangle {
        width: 30;
        height: 30;
        id: rect;
        color: "black";
        // 对象的水平中心 以 pic的水平中心 为中心
        anchors.horizontalCenter: pic.horizontalCenter;
        // 注意:horizomtalCenterOffset针对于horizontalCenter
        anchors.horizontalCenterOffset: 50;
    }
}

图片.png

  • anchors.verticalCenterOffset : real [可读写][参照设horizontalCenter,与其类似]
  • anchors.baselineOffset : real[可读写][参照设horizontalCenter,与其类似]
  • anchors.alignWhenCentered : bool [可读写][指定不使用半个像素绘制图形,当需要居中一个elements,宽度或者高度是基数,不使用半个像素绘制,对此处理解有疑问]

 

原博主博客地址:https://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

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



相关文章
|
计算机视觉 容器
Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
|
数据可视化 Swift 开发者
零一万物开源Yi系列“理科状元”Yi-9B,消费级显卡可跑,魔搭社区最佳实践
零一万物发布并开源了Yi系列中的“理科状元”——Yi-9B,可在魔搭体验
|
关系型数据库 MySQL API
|
存储 边缘计算 安全
边缘计算的概念和在IoT中的应用
随着物联网(IoT)设备数量的激增,传统的云计算模式面临着数据传输延迟和带宽压力等问题。边缘计算作为一种新的计算模式,通过将计算资源和服务部署到靠近数据源的位置,解决了这些问题。
261 2
|
存储 关系型数据库 MySQL
MySQL 索引结构及其优劣
【10月更文挑战第12天】不同的索引结构各有其适用场景,在实际应用中,需要根据数据特点、查询需求等因素综合考虑选择合适的索引结构。同时,过多或不合理的索引也可能会带来一些负面影响,如增加存储开销、降低数据插入和更新的速度等。因此,在设计索引时需要进行合理的规划和优化。
293 57
|
11月前
|
人工智能 自然语言处理 算法
更快、更强、更经济!港大开源大模型RAG系统LightRAG
香港大学研究团队推出LightRAG,一款新型检索增强生成系统。LightRAG通过引入图结构优化文本索引和检索,克服了传统RAG系统在上下文感知、数据表示和更新效率方面的局限。其双级检索系统、图结构与向量表示的融合及增量更新算法,显著提升了检索准确性和效率,适用于智能客服、知识问答和智能搜索等多个领域。
498 3
|
11月前
|
负载均衡 Java API
小红书商品详情API接口获取步骤
小红书商品详情API接口使用指南:先注册并实名认证获取权限,阅读API文档了解使用方法;通过编程调用API,构建请求参数,处理返回数据;注意高并发下的性能优化,确保安全合规;申请API权限,查阅文档,完成开发与调试。
|
网络协议 网络架构
【IP协议】解决 IP 地址不够用的问题(IP地址管理:动态分配、NAT、Ipv6)
【IP协议】解决 IP 地址不够用的问题(IP地址管理:动态分配、NAT、Ipv6)
514 1
|
机器学习/深度学习 存储 算法
技术好文:ttf文件结构解析
技术好文:ttf文件结构解析
627 0
|
JavaScript 前端开发 C++
打造卓越 QML 层级设计:从入门到精通(二)
打造卓越 QML 层级设计:从入门到精通(二)
1615 0