QML Gradient(渐变)

news2024/10/5 22:11:52

在Rectangle中简单的介绍了渐变,但只介绍了一种,下面还会介绍几种。

注意:渐变(Gradient)的优先级大于普通颜色(color)

线性渐变:

默认(从上到下)垂直渐变:

格式:
gradient: Gradient{
            GradientStop{position: xxx;color:"xxx"}
        }
  • gradient:Gradient   代表线性的垂直渐变
  • GradientStop  确定位置和颜色
  • position:位置(0.0-1)
  • color:颜色

例子:

Rectangle
    {
        id:rect1 //标识符
        x:100
        y:100 
        width:100
        height: 100
        gradient: Gradient{
            GradientStop{position:0.0;color: "red"}
            GradientStop{position:0.5;color: "green"}
            GradientStop{position:1;color: "blue"}
        }
    

 

LinearGradient (线性梯度渐变 )

LinearGradient来源于QtGraphicalEffects 所以要添加:

import QtGraphicalEffects 1.15   (版本号根据情况设置)

操作过程:

使用 start:Qt.point(x,y)和end:Qt.point(x,y)来操纵渲染区域

  • start:Qt.point(x,y)
  • end: Qt.point(X,Y)
    • 当 x=X 时是 垂直线性渐变
    • 当 y=Y 时是 水平线性渐变
    • 都不相等时,是斜对角线性渐变
    • 都相同时,显示第一种颜色
  • start代表开始位置,end代表结束的位置
cached 是否缓存提高渲染性能(默认false)

基本格式:

 import QtGraphicalEffects 1.0

LinearGradient {//线性梯度渐变
        anchors.fill: parent//填充父类
        start: Qt.point(0, 0)//起始点
        end: Qt.point(0, 0)//结束点
        gradient: Gradient {//线性渐变
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 1.0; color: "black" }
        }
    }

垂直线性渐变:

Rectangle
    {
        width:100
        height: 100
        LinearGradient{
            anchors.fill:parent
            start:Qt.point(0,0)
            end:Qt.point(0,100)
            gradient: Gradient{
                GradientStop{position:0.0;color:"red"}
                GradientStop{position:0.5;color:"green"}
                GradientStop{position:1.0;color:"blue"}
            }

        }
    }

 水平渐变:

Rectangle
    {
        width:100
        height: 100
        LinearGradient{
            anchors.fill:parent
            start:Qt.point(0,100)
            end:Qt.point(100,100)
            gradient: Gradient{
                GradientStop{position:0.0;color:"red"}
                GradientStop{position:0.5;color:"green"}
                GradientStop{position:1.0;color:"blue"}
            }

        }
    }

 斜对角渐变:

Rectangle
    {
        width:100
        height: 100
        LinearGradient{
            anchors.fill:parent
            start:Qt.point(0,50)
            end:Qt.point(50,0)
            gradient: Gradient{
                GradientStop{position:0.0;color:"red"}
                GradientStop{position:0.5;color:"green"}
                GradientStop{position:1.0;color:"blue"}
            }

        }
    }

 两个坐标相同时,显示一种颜色:

Rectangle
    {
        width:100
        height: 100
        LinearGradient{
            anchors.fill:parent
            start:Qt.point(50,50)
            end:Qt.point(50,50)
            gradient: Gradient{
                GradientStop{position:0.0;color:"red"}
                GradientStop{position:0.5;color:"green"}
                GradientStop{position:1.0;color:"blue"}
            }

        }
    }

 

 ConicalGradient 圆锥梯度

ConicalGradient 来源于QtGraphicalEffects 所以要添加:

import QtGraphicalEffects 1.15   (版本号根据情况设置)

渐变由两种或多种颜色定义,这些颜色无缝混合。颜色从指定的角度开始,以 360 度较大的角度值结束。

angle角度
cached缓存
gradient渐变
horizontalOffset水平偏移
source资源,从资源处开始渐变
verticlOffset垂直偏移

 基本格式:

 ConicalGradient {//圆锥渐变
        anchors.fill: parent
        angle: 0.0 //角度
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 1.0; color: "black" }
        }
    }

设置渐变角度:

Rectangle{
        x:200
        y:200
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            cached: true
            angle:50//设置渐变角度
            gradient: Gradient{
                GradientStop{position:0.0;color: "blue"}
                GradientStop{position:1.0;color: "black"}
            }

        }

    }

 水平偏移和竖直偏移:水平偏移和竖直偏移默认为0,在最中心。

水平偏移:

  •  horizontalOffset:-50 //向左偏移50
  •  horizontalOffset: 50 //向右偏移50
Rectangle{
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            cached: true
            angle:0
            horizontalOffset:-50 //向左偏移50
            verticalOffset:0
            gradient: Gradient{
                GradientStop{position:0.0;color: "blue"}
                GradientStop{position:1.0;color: "black"}
            }
        }
    }
    Rectangle{
        x:200
        y:000
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            cached: true
            angle:0
            horizontalOffset:50//向右偏移50
            verticalOffset:0
            gradient: Gradient{
                GradientStop{position:0.0;color: "blue"}
                GradientStop{position:1.0;color: "black"}
            }
        }
    }

 垂直偏移:

  • verticalOffset:-50 向上偏移50
  • verticalOffset:50  向下偏移50
Rectangle{
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            cached: true
            angle:90
            horizontalOffset:0 //向左偏移50
            verticalOffset:-50
            gradient: Gradient{
                GradientStop{position:0.0;color: "blue"}
                GradientStop{position:1.0;color: "black"}
            }
        }
    }
    Rectangle{
        x:200
        y:000
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            cached: true
            angle:90
            horizontalOffset:0//向右偏移
            verticalOffset:50
            gradient: Gradient{
                GradientStop{position:0.0;color: "blue"}
                GradientStop{position:1.0;color: "black"}
            }
        }
    }

 资源的渐变:(图片)

下载透明背景图的地址:下载推特徽标透明PNG - StickPNG

Rectangle{
        width: 200
        height:200
        ConicalGradient
        {
            anchors.fill:parent
            source:Image
            {
                width:200
                height:200
                source:"/image/580b57fcd9996e24bc43c540.png"
            }
        }
    }

原图: 

 运行后:

 

 RadialGradient 径向梯度渐变

RadialGradient 来源于QtGraphicalEffects 所以要添加:

import QtGraphicalEffects 1.15   (版本号根据情况设置)

 渐变由两种或多种颜色定义,这些颜色无缝混合。颜色从项目的中间开始,到边框结束。

常用的属性: 

angle角度
cached缓存
gradient渐变
horizontalOffset水平偏移
horizontalRadius水平半径
source来源
verticalOffset垂直偏移
verticalRadius垂直半径

 格式为:

RadialGradient {//径向渐变
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 0.5; color: "black" }
        }
    }

 这些参数的使用:

角度、垂直和竖直半径和偏移量的使用

Rectangle{
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            angle:50//角度
            horizontalRadius:100;//水平半径
            verticalRadius:50//垂直半径
            gradient: Gradient {
                GradientStop { position: 0.0; color: "white" }
                GradientStop { position: 0.5; color: "black" }
            }
        }
    }
    Rectangle{
        x:200
        y:0
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            angle:90//角度
            horizontalRadius:50;//水平半径
            verticalRadius:100//垂直半径
            gradient: Gradient {
                GradientStop { position: 0.0; color: "white" }
                GradientStop { position: 0.5; color: "black" }
            }
        }
    }
    Rectangle{
        x:400
        y:0
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            angle:50//角度
            horizontalRadius:100;//水平半径
            horizontalOffset: 50;//水平偏移
            verticalRadius:50//垂直半径
            verticalOffset:50//垂直半径
            gradient: Gradient {
                GradientStop { position: 0.0; color: "white" }
                GradientStop { position: 0.5; color: "black" }
            }
        }
    }

 渐变的使用

Rectangle{
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            gradient: Gradient {
                GradientStop {
                 position: 0.000
                 color: Qt.rgba(1, 0, 0, 1)
                }
                GradientStop {
                 position: 0.167
                 color: Qt.rgba(1, 1, 0, 1)
                }
                GradientStop {
                 position: 0.333
                 color: Qt.rgba(0, 1, 0, 1)
                }
                GradientStop {
                 position: 0.500
                 color: Qt.rgba(0, 1, 1, 1)
                 }
                GradientStop {
                 position: 0.667
                 color: Qt.rgba(0, 0, 1, 1)
                }
                GradientStop {
                 position: 0.833
                 color: Qt.rgba(1, 0, 1, 1)
                }
                GradientStop {
                 position: 1.000
                 color: Qt.rgba(1, 0, 0, 1)
                }
              }
            }
        }

 添加资源:(图片)

Rectangle{
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            source:Image
            {
                width:200
                height:200
                source:"/image/580b57fcd9996e24bc43c540.png"
            }
            gradient: Gradient
            {
                GradientStop{position:0.0;color: "red"}
                GradientStop{position:0.5;color: "blue"}
                GradientStop{position:1.0;color: "green"}
            }
        }
    }

 最后来一个综合使用:

Rectangle{
        width: 200
        height:200
        RadialGradient {
            anchors.fill: parent
            angle:50;
            horizontalRadius:100
            horizontalOffset:50
            verticalRadius:100
            verticalOffset:-50
            source:Image
            {
                width:200
                height:200
                source:"/image/580b57fcd9996e24bc43c540.png"
            }
            gradient: Gradient
            {
                GradientStop{position:0.0;color: "red"}
                GradientStop{position:0.5;color: "blue"}
                GradientStop{position:1.0;color: "green"}
            }
        }
    }

 

参考文档:

线性梯度 QML |型Qt 图形效果 5.15.12 

ConicalGradient QML Type | Qt Graphical Effects 5.15.12

RadialGradient QML Type | Qt Graphical Effects 5.15.12

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/350579.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++【map和set的基本使用】

文章目录1、关联式容器2、键值对3、树形结构的关联式容器3-1、set3-1-1、set的使用3-1-3、set的使用样例3-2、map3-2-1、map的使用3-2-2、map的使用样例3-3、multiset3-4、multimap4、总结1、关联式容器 在初阶阶段,我们已经接触过STL中的部分容器,比如…

使用IPV6+DDNS连接内网主机

0、前言 IPV6已经普及多年,但是作为互联网用户好像并没有在实用性上有更多感受,或者说IPV6并没有让普通用户感觉到改变。我作为网络从业者其实也没有过多关注。在工作中普遍遇到的还是基于IPV4的网络,比如各个行业的网络、单位的内网区域和互…

C语言赋值(关系)运算符和逗号运算符

一.赋值&#xff08;关系&#xff09;运算符 1.关系运算符 高优先级组 < 左边值小于右边值,则返回1。否则返回0 < 左边值小于等于右边值,则返回1。否则返回0 > 左边值大于右边值,则返回1。否则返回0 > 左边值大于等于右边值,则返回1。否则返回0 低优先级组…

React组件的用法和理解

React组件 函数式组件 <div id"test"></div><script type"text/babel">//1.创建函数式组件(必须大写&#xff0c;函数必须有返回值)function MyComponent(){console.log(this); //此处的this是undefined&#xff0c;因为babel编译后开…

自己实现 ChatGpt ?先学习 Pytorch 吧

最近 ChatGpt 的爆火&#xff0c;让人非常震撼&#xff0c;无论是知识问答、对话还是代码撰写&#xff0c;都非常符合人们的预期&#xff0c;让人不得不感慨机器学习的强大。不信&#xff1f;看下面&#xff1a; 图1 语言分析处理 图2 知识问答 图3 写故事 图4 写代码 体…

详解ZabbixBSM是啥?业务服务监控达到全新高度

感谢本文译者赵广生 ! 欢迎更多资深用户&#xff0c;结合自己使用经验翻译原厂博文&#xff01; 赵广生 毕业于北京外国语大学&#xff08;信息管理与信息技术&#xff09;&#xff1b; 毕业后先后在多个公司从事信息化运维管理工作&#xff1b; 主要涉及虚拟化领域vmwa…

pytorch离线安装

windows下离线安装pytorch&#xff0c;很多内网机&#xff0c;无法连接外网&#xff0c;只能下载whl文件进行离线安装下载pytorch&#xff0c;地址https://download.pytorch.org/whl/torch_stable.html我是windows&#xff0c;Python37&#xff0c;没有gpu&#xff0c;所以选择…

如何利用知识库加强内部管理?

许多公司都知道需要有一个面向客户的知识库&#xff0c;以加强客户服务&#xff0c;提供更好的客户体验。 但是很多企业没有意识到的是&#xff0c;拥有一个内部知识库软件对于员工改善沟通和促进知识共享的重要性。 协作是组织成功的关键部分&#xff0c;通过明确的远景和使…

怎么看电脑显卡?3个步骤,1分钟学会

显卡作为电脑重要的组成部件&#xff0c;具有重要的作用。很多小伙伴买回来电脑后&#xff0c;想要查看电脑显卡&#xff0c;却不知道怎么看电脑显卡&#xff1f;别着急&#xff0c;今天小编就应各位小伙伴的要求&#xff0c;以图文的方式&#xff0c;3个步骤教你如何看电脑显卡…

C++设计模式(15)——代理模式

亦称&#xff1a; Proxy 意图 代理模式是一种结构型设计模式&#xff0c; 让你能够提供对象的替代品或其占位符。 代理控制着对于原对象的访问&#xff0c; 并允许在将请求提交给对象前后进行一些处理。 问题 为什么要控制对于某个对象的访问呢&#xff1f; 举个例子&…

分享117个HTML婚纱模板,总有一款适合您

分享117个HTML婚纱模板&#xff0c;总有一款适合您 117个HTML婚纱模板下载链接&#xff1a;https://pan.baidu.com/s/1cC3I5cfh91-KmQj4nfSoPA?pwd9hod 提取码&#xff1a;9hod Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 import os import shutil import …

懂九转大肠的微软New Bing 内测申请教程

最近微软的New Bing开放内测了&#xff0c;网上已经有拿到内测资格的大佬们对比了ChatGPT和New Bing。对比结果是New Bing比ChatGPT更强大。来看看具体对比例子吧 1.时效性更强 ChatGPT的库比较老&#xff0c;跟不上时事&#xff0c;比如你问它九转大肠的梗&#xff0c;ChatG…

初级调色转档CameraRaw

一级调色 还原-曝光-色彩-细节-质感 修图的范围 整体&#xff08;掌握基本面板&#xff09;——局部&#xff08;曲线&#xff09;——具象&#xff08;混色器&#xff09; 修片最开始的准备工作 看直方图:明暗跟色彩的数据表 分析图片是否存在以下问题&#xff1a; 1.曝光…

Linux权限概念

目录 Linux权限的概念 什么是权限 如何去操作权限 设置文件所属角色 设置文件属性 umask 粘滞位 Linux权限的概念 首先我们要了解到&#xff0c;在linux下有两种用户&#xff1a;超级用户(root)和普通用户。超级用户的命令提示符是“#”&#xff0c;普通用户的命令提示…

亚马逊测评自养号需要注意的五点

一.有自己的一套环境系统市场上有很多系统&#xff0c;但很多都是现成的或软件包&#xff0c;没有解决风控的能力&#xff0c;这个还需要大家自己甄别的哈&#xff0c;如果有需要建议大家自己学习一套&#xff0c;把技术掌握在自己手里&#xff0c;这样不会有依赖性&#xff0c…

零基础、学历无优势、逻辑能力一般”,能转行做程序员吗?

此前&#xff0c;拉勾数据研究院对程序员群体做了一次深入调查&#xff0c;并发布了《2022程序员群体职场洞察报告》&#xff0c;报告显示&#xff0c;“高薪”依然是程序员的职业标签之一。 在调查的程序员群体中&#xff0c;年薪在10-30万元之间的人数占比为66.7%&#xff0…

V4l2框架基础知识(三)

V4L2框架概述 V4L2框架主要部分组成&#xff1a; V4L2_device&#xff1a;管理所有设备&#xff1b; media_device:media_device框架管理运行时的pipeline&#xff1b; V4L2_device 这个是整个输入设备的总结构体&#xff0c;可以认为他是整个V4L2框架的入口&#xff0c;由该…

分享53戏源代码总有一个是你想要的(亲测每一个均可用)

收集整理不容易老铁支持我动力&#xff01; 53戏源代码下载链接&#xff1a;https://pan.baidu.com/s/1MI_EvHNFKoUu-l8LR6MAmg?pwdass5 提取码&#xff1a;ass5 下面是项目的名字&#xff0c;我放了一些图片&#xff0c;大家下载后可以看到。 c#版植物大战僵尸 H5纸牌接龙…

猿人学 第十六题

使用XHR断点找到XHR发送位置向上跟栈&#xff0c;会发现在ajax中发现m已经生成&#xff0c;向上追踪代码继续向上追&#xff0c;发现m在i中出现&#xff0c;并在r.m中生成进入n[e(528)]方法&#xff0c;发现作用只是返回e(t)p_s为获取当时时间的方法 btoa为方法点击进入btoa方法…

玩机搞机------高级改良型高通工程线制作方法。

很多普通玩家对工程线的定义还停留在早期短接红绿端的操作认知上。例如这个 常规制作工程线 或者短接主板触点方式 今天给大家描述的是更高级改良型的工程线。因为前期的这种工程线对于米6或者其他同时期机型已经不起任何作用。后期机型进入高通edl模式就需要拆机手动短接主板触…