QML Rectangle 组件

基本属性

属性 类型 默认值 描述
color color "white" 矩形填充颜色
border.color color "transparent" 边框颜色
border.width int 0 边框宽度
radius real 0 圆角半径
gradient Gradient null 渐变填充
antialiasing bool true 是否抗锯齿

几何属性 (继承自Item)

属性 类型 默认值 描述
x real 0 X坐标位置
y real 0 Y坐标位置
width real 0 宽度
height real 0 高度
z real 0 Z轴堆叠顺序
rotation real 0 旋转角度(度)
scale real 1.0 缩放比例
opacity real 1.0 透明度(0.0-1.0)

方法 (继承自Item)

方法 参数 返回值 描述
mapToItem(Item item, real x, real y) item: 目标项目 x,y: 坐标 point 坐标转换到另一个item
mapFromItem(Item item, real x, real y) item: 源项目 x,y: 坐标 point 从另一个item转换坐标
contains(point) point: 要检查的点 bool 检查点是否在矩形内
forceActiveFocus() - - 强制获取键盘焦点

信号 (继承自Item)

信号 参数 描述
xChanged() - X位置改变时触发
yChanged() - Y位置改变时触发
widthChanged() - 宽度改变时触发
heightChanged() - 高度改变时触发
colorChanged() - 颜色改变时触发
borderChanged() - 边框属性改变时触发

基本用法示例

qml

复制代码
import QtQuick 2.15

Rectangle {
    id: rect
    width: 100
    height: 100
    color: "blue"
    border.color: "black"
    border.width: 2
    radius: 10
    
    // 渐变填充
    gradient: Gradient {
        GradientStop { position: 0.0; color: "blue" }
        GradientStop { position: 1.0; color: "lightblue" }
    }
    
    // 旋转动画
    RotationAnimation on rotation {
        from: 0
        to: 360
        duration: 2000
        loops: Animation.Infinite
    }
}

圆角矩形示例

qml

复制代码
Rectangle {
    width: 150
    height: 80
    color: "#3498db"
    radius: height/2  // 完全圆角(胶囊形状)
    border {
        color: "#2980b9"
        width: 2
    }
}

带阴影的矩形

qml

复制代码
Rectangle {
    id: shadowRect
    width: 120
    height: 120
    color: "white"
    radius: 8
    
    // 阴影效果
    layer.enabled: true
    layer.effect: DropShadow {
        transparentBorder: true
        horizontalOffset: 3
        verticalOffset: 3
        radius: 8.0
        samples: 16
        color: "#80000000"
    }
}
复制代码
相关推荐
Wallace Zhang1 天前
PySide6 + QML - 多线程02 - QThread 生命周期与安全退出
vscode·pyside6·qml
江公望2 天前
如何在Qt QML中定义枚举浅谈
开发语言·qt·qml
Hi202402173 天前
Qt+Qml客户端和Python服务端的网络通信原型
开发语言·python·qt·ui·网络通信·qml
江公望7 天前
Qt qmlplugindump浅谈
开发语言·qt·qml
江公望11 天前
Qt的QT_QPA_EGLFS_INTEGRATION环境变量浅解
linux·qt·qml
江公望15 天前
通过QQmlExtensionPlugin进行Qt QML插件开发
c++·qt·qml
江公望19 天前
Qt QtConcurrent使用入门浅解
c++·qt·qml
ajassi200022 天前
开源 C++ QT QML 开发(二)工程结构
linux·qt·qml
ajassi20001 个月前
开源 C++ QT QML 开发(一)基本介绍
linux·qt·开源·qml
大橘1 个月前
【qml-11】Quick3D实现机器人欧拉旋转、拖动视角
qt·3d·机器人·qml