QML 矩形颜色横向渐变(gradient和LinearGradient)


所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多的时间吧!

QML其它文章请点击这里:     QT QUICK QML 学习笔记


1. gradient 颜色渐变

常用 gradient 来实现渐变,如下:

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow{
    id:             root
    visible:        true
    width:          300
    height:         200
    title:          qsTr("颜色渐变");
    Rectangle {
        id:                     rect
        anchors.fill:           parent
        border.width:           1
        border.color:           "black"
        gradient: Gradient {
            GradientStop {  position: 0.0;    color: "blue"  }
            GradientStop {  position: 1.0;    color: "green" }
        }
       	Text {
            text: 				qsTr("我是竖向渐变")
            color:              "white"
            anchors.centerIn:   parent
        }
    }
}

在这里插入图片描述
如果想实现横向渐变的话,可以通过增加旋转,如下:

    Rectangle {
        id:                     rect
        anchors.fill:           parent
        border.width:           1
        border.color:           "black"
        rotation:               270  	///--[Mark]
        gradient: Gradient {
            GradientStop {  position: 0.0;    color: "blue"  }
            GradientStop {  position: 1.0;    color: "green" }
        }
        ...
    }

在这里插入图片描述
enmmm,颜色是旋转了,但是其它都不得劲了,所以还是别旋转了,一转就都转,下面介绍另外的一种方法。

2. LinearGradient 线性渐变

LinearGradient 线性渐变,它可以从给定的起始点开始,到给定的结束点结束,使几种颜色无缝衔接渐变,代码和效果如下:

    LinearGradient {            ///--[Mark]
        anchors.fill: parent
        start: Qt.point(0, 0)   
        end: Qt.point(width, 0)        ///1、横向渐变
//        end: Qt.point(0, height)     ///2、竖向渐变
//        end: Qt.point(width, height) ///3、横向渐变
        gradient: Gradient {
            GradientStop {  position: 0.0;    color: "red" }
            GradientStop {  position: 0.5;    color: "blue" }
            GradientStop {  position: 1.0;    color: "green" }
        }
    }

图像如下:
在这里插入图片描述
完整代码如下:记得需要导入: import QtGraphicalEffects 1.12

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtGraphicalEffects 1.12      ///--[Mark]

ApplicationWindow{
    id:             root
    visible:        true
    width:          300
    height:         200
    title:          qsTr("颜色渐变");

    Rectangle {
        id:                     rect
        anchors.fill:           parent
        border.width:           1
        border.color:           "black"
        LinearGradient {            ///--[Mark]
            anchors.fill: parent
            start: Qt.point(0, 0)
//            end: Qt.point(width, 0)      ///1.横向渐变
//            end: Qt.point(0, height)     ///2.竖向渐变
            end: Qt.point(width, height) ///3.斜向渐变
            gradient: Gradient {
                GradientStop {  position: 0.0;    color: "red" }
                GradientStop {  position: 0.5;    color: "green" }
                GradientStop {  position: 1.0;    color: "blue" }
            }
        }
        Text {
            text:               qsTr("1.横向渐变")
            color:              "white"
            anchors.centerIn:    parent
        }
    }
}

这是对矩形的颜色渐变,如果是图像呢? LinearGradient 线性渐变也是神器,你不点下么?QML 图像颜色渐变和颜色覆盖(LinearGradient和ColorOverlay)


QML其它文章请点击这里:     QT QUICK QML 学习笔记

  • 30
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值