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"
    }
}
复制代码
相关推荐
人还是要有梦想的5 天前
QT qml布局讲解
qt·布局·qml
Rookie Linux8 天前
使用Qt6 QML以及第三方库FluentUI、PCapPlusPlus开发一个自定义抓包软件
网络·c++·qt·cmake·qml
谁刺我心10 天前
[QtCPP]Examples使用示例-QtMultimedia、QMediaPlayer、Audio音频引擎测试mp3播放
qt·音视频·qml
Quz10 天前
Qt Quick 粒子系统(二):系统控制与生命周期管理
qt·qml·粒子系统
Quz10 天前
Qt Quick 粒子系统(三):发射器深度解析
qt·qml·粒子系统
nnnnichijou12 天前
Qt 6.9 嵌入式 Linux 交叉编译全栈填坑指南(以树莓派5 AArch64 为例
qt·嵌入式·交叉编译·qml·树莓派5
Quz13 天前
Qt Quick 粒子系统(一):架构总览与四层模型
qt·架构·qml
Ulyanov19 天前
深入QML-Python通信 构建响应式交互界面的桥梁设计:QML+PySide6现代开发入门(五)
开发语言·python·算法·交互·qml·系统仿真
东方.既白1 个月前
QML简易地铁导乘屏
qml
Ulyanov1 个月前
PySide6 + QML 混合编程全景解析:从底层原理到企业级实战
python·pyside6·qml·雷达电子对抗