qml Item 元素

Item 元素

Item 是 QML 中最基本的可视化组件之一,它是所有可视化组件的基类。
Item 本身不绘制任何内容,但提供了布局、定位和事件处理等基本功能。

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Item {
        // 基本属性
        x: 100  // 设置 Item 的 x 坐标,即左上角的水平位置
        y: 100  // 设置 Item 的 y 坐标,即左上角的垂直位置
        width: 200  // 设置 Item 的宽度
        height: 100  // 设置 Item 的高度
        visible: true  // 控制 Item 是否可见,默认为 true
        enabled: true  // 控制 Item 是否启用,默认为 true
        opacity: 0.8  // 设置 Item 的不透明度,范围从 0.0(完全透明)到 1.0(完全不透明)

        // 布局属性
        anchors.centerIn: parent  // 将 Item 居中对齐到父组件
        anchors.fill: parent  // 使 Item 填充父组件
        anchors.left: parent.left  // 将 Item 的左边缘对齐到父组件的左边缘
        anchors.right: parent.right  // 将 Item 的右边缘对齐到父组件的右边缘
        anchors.top: parent.top  // 将 Item 的上边缘对齐到父组件的上边缘
        anchors.bottom: parent.bottom  // 将 Item 的下边缘对齐到父组件的下边缘
        anchors.horizontalCenter: parent.horizontalCenter  // 将 Item 的水平中心对齐到父组件的水平中心
        anchors.verticalCenter: parent.verticalCenter  // 将 Item 的垂直中心对齐到父组件的垂直中心

        // 事件处理属性
        focus: true  // 控制 Item 是否获得焦点,获得焦点的 Item 可以接收键盘事件
        hoverEnabled: true  // 控制 Item 是否启用悬停事件,启用后可以接收鼠标悬停事件

        // 其他属性
        z: 1  // 设置 Item 的堆叠顺序,数值越大,Item 越靠近用户
        clip: true  // 控制 Item 是否裁剪其子组件,设置为 true 时,超出 Item 边界的子组件部分将被裁剪
        transform: [  // 用于对 Item 进行变换(如旋转、缩放、平移等)
            Rotation { angle: 45 }  // 旋转 45 度
            Scale { x: 2; y: 2 }  // 放大 2 倍
            Translate { x: 10; y: 10 }  // 平移 10 像素
        ]
    
        

        // 子组件
        Rectangle {
            width: 100  // 设置 Rectangle 的宽度
            height: 100  // 设置 Rectangle 的高度
            color: "red"  // 设置 Rectangle 的颜色为红色
            anchors.centerIn: parent  // 将 Rectangle 居中对齐到父 Item
            
            // 边框属性
            border.color: "black"  // 设置 Item 的边框颜色
            border.width: 1  // 设置 Item 的边框宽度
            radius: 5  // 设置 Item 的圆角半径


            gradient: Gradient {  // 设置 Item 的渐变色
                GradientStop { position: 0.0; color: "red" }
                GradientStop { position: 1.0; color: "blue" }
            }
        }

        Text {
            text: "Hello, Item!"  // 设置 Text 的文本内容为 "Hello, Item!"
            anchors.top: parent.bottom  // 将 Text 定位到父 Item 的底部
            anchors.horizontalCenter: parent.horizontalCenter  // 将 Text 水平居中对齐到父 Item
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

可能只会写BUG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值