【Qt】QML元素:Item

本文详细介绍了QtQuick中Item组件的功能,包括作为容器、事件处理、水平镜像及透明度设置。Item作为不可见的基础元素,可承载多个子元素并能处理输入事件。同时,展示了如何通过LayoutMirroring属性实现镜像效果,以及如何利用opacity属性调整透明度。此外,列举了Item的重要属性,如锚点布局、尺寸和位置等。
摘要由CSDN通过智能技术生成

Qt Quick所有的可见的组件都是继承自Item,但Item对象是不可见的,但是它可以像其他可见组件一样定义x、y、width和 height等属性。

一、功能

1 容器

Item元素可以起到容器的作用,将多个元素放到一个元素内。

import QtQuick 2.0

Item {
    Image {
        source: "tile.png"
    }
    Image {
        x: 80
        width: 100
        height: 100
        source: "tile.png"
    }
    Image {
        x: 190
        width: 100
        height: 100
        fillMode: Image.Tile
        source: "tile.png"
    }
}

2 事件处理

对输入信号进行事件处理,例如鼠标、触摸板和按键等。

对于鼠标,如:鼠标移动、鼠标按下、鼠标释放等。

对于键盘,如:按键按下、按键释放等。

import QtQuick 2.0

Item {
    focus: true
    Keys.onPressed: {
        if (event.key == Qt.Key_Left) {
            console.log("move left");
            event.accepted = true;
        }
    }
    Keys.onReturnPressed: console.log("Pressed return");
}

3 水平镜像

可通过LayoutMirroring属性对所包含的元素进行。

LayoutMirroring.enabled: true:开启镜像
LayoutMirroring.childrenInherit: true:子元素镜像

import QtQuick 2.0

Rectangle {
    LayoutMirroring.enabled: true
    LayoutMirroring.childrenInherit: true

    width: 300; height: 50
    color: "yellow"
    border.width: 1

    Row {
        anchors { left: parent.left; margins: 5 }
        y: 5; spacing: 5

        Repeater {
            model: 5

            Rectangle {
                color: "red"
                opacity: (5 - index) / 5
                width: 40; height: 40

                Text {
                    text: index + 1
                    anchors.centerIn: parent
                }
            }
        }
    }
}

在这里插入图片描述

4 透明度

在这里插入图片描述

Item {
    id: nonLayered

    opacity: 0.5    

	layer.enabled: false

    width: 100
    height: 100

    Rectangle { width: 80; height: 80; border.width: 1 }
    Rectangle { x: 20; y: 20; width: 80; height: 80; border.width: 1 }
}

在这里插入图片描述

Item {
    id: layered

    opacity: 0.5

    layer.enabled: true

    width: 100
    height: 100

    Rectangle { width: 80; height: 80; border.width: 1 }
    Rectangle { x: 20; y: 20; width: 80; height: 80; border.width: 1 }
}

二、属性

  • activeFocus : bool
  • activeFocusOnTab : bool
  • anchors
    • anchors.alignWhenCentered : bool
    • anchors.baseline : AnchorLine
    • anchors.baselineOffset : real
    • anchors.bottom : AnchorLine
    • anchors.bottomMargin : real
    • anchors.centerIn : Item
    • anchors.fill : Item
    • anchors.horizontalCenter : AnchorLine
    • anchors.horizontalCenterOffset : real
    • anchors.left : AnchorLine
    • anchors.leftMargin : real
    • anchors.margins : real
    • anchors.right : AnchorLine
    • anchors.rightMargin : real
    • anchors.top : AnchorLine
    • anchors.topMargin : real
    • anchors.verticalCenter : AnchorLine
    • anchors.verticalCenterOffset : real
  • antialiasing : bool
  • baselineOffset : int
  • children : list
  • childrenRect
    • childrenRect.height : real
    • childrenRect.width : real
    • childrenRect.x : real
    • childrenRect.y : real
  • clip : bool
  • containmentMask : QObject*
  • data : list
  • enabled : bool
  • focus : bool
  • height : real
  • implicitHeight : real
  • implicitWidth : real
  • layer.effect : Component
  • layer.enabled : bool
  • layer.format : enumeration
  • layer.mipmap : bool
  • layer.samplerName : string
  • layer.samples : enumeration
  • layer.smooth : bool
  • layer.sourceRect : rect
  • layer.textureMirroring : enumeration
  • layer.textureSize : size
  • layer.wrapMode : enumeration
  • opacity : real
  • parent : Item
  • resources : list
  • rotation : real
  • scale : real
  • smooth : bool
  • state : string
  • states : list
  • transform : list
  • transformOrigin : enumeration
  • transitions : list
  • visible : bool
  • visibleChildren : list
  • width : real
  • x : real
  • y : real
  • z : real

参考:https://doc.qt.io/qt-5/qml-qtquick-item.html#layout-mirroring

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天边星宿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值