Qml——Rectangle元素

1.rectangle元素是用来绘制实心矩形

复制代码
Rectangle{
        x: 30
        y: 30
        color: "red" //矩形填充色
        width: 100
        height: 100
        border.color: "green"
        border.width: 5
        radius: 20 //四个角的圆角半径
        antialiasing: true //抗锯齿,让圆角边缘平滑
        rotation: 45 //相对元素中心点顺时针旋转45°
    }

运行结果:

2.rectangle线性渐变

gradient属性默认是从上往下渐变,通过GrandientStop制定每个位置上的颜色,例如:

复制代码
Rectangle{
        x: 30
        y: 30
        color: "red" //矩形填充色
        width: 100
        height: 100
        radius: 20
        gradient: Gradient{
            GradientStop { position: 0.0; color: "red"}
            GradientStop { position: 0.5; color: "yellow"}
            GradientStop { position: 1.0; color: "green"}
        }
    }

运行结果:

如果想要实现水平渐变、斜角渐变,则要使用LinearGradient对象,必须使用start和end属性来指定渐变的方向

复制代码
start: Qt.point(int x1, int y1)
end: Qt.point(int x2, int y2)

如果x1=x2,则表示垂直渐变

如果y1=y2,则表示平行渐变

如果都不相等,则表示斜角线性渐变

复制代码
Rectangle{
        x: 30
        y: 30
        color: "red" //矩形填充色
        width: 100
        height: 100
        radius: 20
        antialiasing: true; //抗锯齿
        LinearGradient{
            anchors.fill: parent //填满整个父元素
            start: Qt.point(0, 0)
            end: Qt.point(parent.width, 0)
            gradient: Gradient{
                GradientStop { position: 0.0; color: "red"}
                GradientStop { position: 0.5; color: "yellow"}
                GradientStop { position: 1.0; color: "green"}
            }
        }
    }

运行结果:

圆锥渐变------ConicalGradient

常用属性:

  • angle:起始的顺时针角度位置
  • horizontalOffset:中心点水平偏移位置
  • verticalOffset:中心点垂直偏移位置
相关推荐
小灰灰搞电子4 小时前
QLibrary 类详解:在运行时与共享库对话
qt·qlibrary
敲上瘾18 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
小此方1 天前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
ShineWinsu1 天前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
bkspiderx1 天前
在Visual Studio中安装与配置Qt插件(Qt Visual Studio Tools)
ide·qt·visual studio·qt插件
ShineWinsu3 天前
对于Qt:信号量(QSemaphore)的解析
c++·qt·面试·笔试·客户端·线程安全·信号量
ShineWinsu4 天前
对于Qt:条件变量(QWaitCondition)的解析
c++·qt·面试·线程·条件变量·客户端·锁
郝学胜-神的一滴4 天前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
敲上瘾4 天前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
ShineWinsu5 天前
对于Qt:事件分发器以及事件过滤器的解析
c++·qt·面试·gui·客户端·事件过滤器·事件分发器