Qml——Item元素

上一章是介绍了Rectangle元素,这一章介绍Rectangle的父元素------Item元素

1、Item常用属性:

  • z属性:设置每个子项目的堆叠显示顺序,默认值为0,z越大,显示在最上方

    Item
    {
    Rectangle{
    color: "red"
    width: 100
    height: 100
    z: 1
    }
    Rectangle{
    color: "blue"
    width: 100
    height: 100
    x: 50; y: 50
    }
    }

运行结果:

  • clip属性:用来控制是否把子元素超出自身边界的部分裁剪掉。默认是false(子元素可以画到父元素边界外)

    Rectangle{
    width: 100
    height: 100
    border.width: 3
    border.color: "red"
    clip: true
    Rectangle{
    width: 70
    height: 70
    color: "blue"
    }
    Rectangle{
    width: 70
    height: 70
    x: 50
    y: 50
    color: "yellow"
    }
    }

运行结果:

当clip为false时,运行结果为:

2、打印children子元素成员

复制代码
Rectangle{
        id: group
        width: 100
        height: 100
        border.width: 3
        border.color: "#000000"
        clip: true
        Rectangle{
            id: rect1
            width: 70
            height: 70
            color: "red"
        }
        Rectangle{
            id: rect2
            width: 70
            height: 70
            x: 50
            y: 50
            color: "yellow"
            visible: false //不显示
        }
        Component.onCompleted: {
            for(var i in data)
            {
                print("data[" + i + "]: ", data[i])
            }
            print("children length: ", children.length)
            for(i = 0; i<children.length; i++)
            {
                print("children[" + i + "] 坐标:", children[i].x, children[i].y)
            }
        }
    }

打印结果:

qml: data0: QQuickRectangle(0x2671310)

qml: data1: QQuickRectangle(0x2671f10)

qml: children length: 2

qml: children0 坐标: 0 0

qml: children1 坐标: 50 50

Component.onCompleted 是槽函数,在组件及其所有子对象创建完成后触发

3、Anchors锚:通过它来指定每个元素与其他元素之间的位置方向

示例:

复制代码
Rectangle{
        color: "blue"
        width: 240
        height: 80
        Rectangle{
            id: rect1
            color: "red"
            anchors.alignWhenCentered: false
            anchors.centerIn: parent
            anchors.horizontalCenterOffset: -parent.width/3 // 中心往左偏移
            anchors.margins: 10
            width: 50
            height: 50
        }
        Rectangle{
            id: rect2
            color: "yellow"
            anchors.alignWhenCentered: false
            anchors.centerIn: parent
            anchors.margins: 10
            width: 50
            height: 50
        }
        Rectangle{
            id: rect3
            color: "lightsteelblue"
            anchors.centerIn: parent
            anchors.horizontalCenterOffset: parent.width/3
            anchors.margins: 10
            width: 50
            height: 50
        }
    }

运行结果:

4、Keys属性使用

复制代码
Item {
            anchors.fill: parent
            focus: true
            Keys.onSpacePressed: console.log("空格键按下")
    }
相关推荐
慧都产品智能化18 小时前
Qt 表格升级攻略:复用已有编辑器,减少重复开发
c++·qt·编辑器·qt6.3
ShineWinsu18 小时前
对于Qt:QTableWidget常见属性以及QTableWidgetItem类的解析
开发语言·c++·qt·gui·客户端·表格·qtablewidget
ShineWinsu19 小时前
对于Qt:QListWidget常见属性以及QListWidgetItem类的解析
开发语言·c++·qt·客户端·列表·qlistwidget·qlistwidgetitem
ShineWinsu1 天前
对于Qt:QDial常见属性以及信号的解析
开发语言·c++·qt·gui·客户端·旋钮·qdial
宛 禾1 天前
Qt 事件系统完全指南:从事件循环到实战避坑
qt
敲上瘾2 天前
Qt Widgets入门:项目模板解析与Hello World
开发语言·数据库·c++·qt·ui·qt6.3
MOONICK2 天前
QT元数据
开发语言·qt
橙橙笔记3 天前
QT的学习第四部分
linux·qt·学习·音视频
sycmancia3 天前
Qml——信号连接、自定义信号
qt
奇树谦6 天前
Qt Quick 图片加载与 GPU 纹理机制详解:QImage、QQuickTextureFactory、QSGTexture 与异步加载
开发语言·qt·系统架构