控件背景图图形渲染ColorOverly

刚接触QT/QML这块没多久,竟然碰到了就记下来吧,在做界面跳转时,想点击此图形就改变颜色,不需要两张图,该怎么做呢,这就可以用到Qt库自带的ColorOverly了,需要用到的类为:
import QtGraphicalEffects 1.0
官方介绍是这样的:

意思为这种效果类似于把彩色玻璃放在灰度图像上的效果。覆盖层的颜色以RGBA格式给出。
放出官方的例子吧:
import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    width: 300
    height: 300

    Image {
        id: bug
        source: "images/butterfly.png"
        sourceSize: Qt.size(parent.width, parent.height)
        smooth: true
        visible: false
    }

    ColorOverlay {
        anchors.fill: bug
        source: bug
        color: "#80800000"
    }
}

就是用另外的颜色将原来的图形的颜色覆盖。此外,我自己也写了一个自定义控件,可以参考下:
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
//import QGroundControl.Palette 1.0

Item {
property color color: “white” // Image color

property alias asynchronous:        image.asynchronous
property alias cache:               image.cache
property alias fillMode:            image.fillMode
property alias horizontalAlignment: image.horizontalAlignment
property alias mirror:              image.mirror
property alias paintedHeight:       image.paintedHeight
property alias paintedWidth:        image.paintedWidth
property alias progress:            image.progress
property alias smooth:              image.smooth
property alias mipmap:              image.mipmap
property alias source:              image.source
property alias sourceSize:          image.sourceSize
property alias status:              image.status
property alias verticalAlignment:   image.verticalAlignment

width:  image.width
height: image.height

Image {
    id:                 image
    smooth:             true
    mipmap:             true
    antialiasing:       true
    visible:            false
    fillMode:           Image.PreserveAspectFit
    anchors.fill:       parent
    sourceSize.height:  height
}

ColorOverlay {
    anchors.fill:       image
    source:             image
    color:              parent.color
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

rookie三号

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

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

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

打赏作者

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

抵扣说明:

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

余额充值