qml中颜色使用rgb格式进行设置

Qt里面要对界面进行颜色赋值,可以采用十六进制(#FFFFFF)或者Qt.rgba(0.5,0.5,0.5,1)这2种方法,这2个方法使用起来都不是很方便, 无法使用rgb值进行设置,也是比较麻烦,因此想到qml支持js函数的方法,进行将rgb转换成十六进制的格式,然后进行使用

使用的时候可以单独创建一个js文件,将这个转换函数写入到其中.

然后使用

import "CommonFunction.js" as CommonFunctionHandler进行导入js文件

函数调用方式:CommonFunctionHandler.rgb(255,182,193)

 

 

下面使用效果

源码

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    function rgb(r, g, b){
        var ret = (r << 16 | g << 8 | b)
        return ("#"+ret.toString(16)).toUpperCase();
    }

    RowLayout{
        anchors.fill: parent
        spacing: 30
        Label{
            Layout.fillWidth: true
        }

        Repeater{
            model:[rgb(120,120,230),
                rgb(60,120,230),
                rgb(120,0,230),
                rgb(120,120,0),
                rgb(90,120,60)]

            Button{

                background: Rectangle{
                    color:modelData
                    implicitHeight:40
                    implicitWidth:40
                }

                onClicked: {

                    console.log(modelData)
                }
            }
        }
        Label{
            Layout.fillWidth: true
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值