news 2026/5/9 11:51:35

[QML] Qt6/Qt5四大渐变效果实战指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
[QML] Qt6/Qt5四大渐变效果实战指南

一、模块导入

import QtQuick import QtQuick.Shapes 1.8 as QT6Style // Qt6 Shape渐变 import Qt5Compat.GraphicalEffects as QT5Style // Qt5兼容效果渐变

二、四种渐变对比

渐变类型模块效果适用场景
GradientQtQuick线性(水平/垂直)简单背景
LinearGradientQT6Style / QT5Style任意方向线性对角线渐变
ConicalGradientQT5Style锥形旋转雷达、光盘
RadialGradientQT5Style径向扩散光晕、按钮

三、Gradient(基础线性渐变)

Rectangle { gradient: Gradient { orientation: Gradient.Horizontal // 或 Vertical GradientStop { position: 0.0; color: "red" } GradientStop { position: 0.5; color: "yellow" } GradientStop { position: 1.0; color: "green" } } }

限制:只能水平或垂直,不能对角线


四、LinearGradient(高级线性渐变)

QT6Style 版本(配合 Shape)

QT6Style.Shape { width: 400; height: 400 QT6Style.ShapePath { PathRectangle { x: 0; y: 0; width: 400; height: 400 } fillGradient: QT6Style.LinearGradient { x1: 0; y1: 0 // 起点 x2: 400; y2: 400 // 终点(对角线) // x2: 0; y2: 400 // 垂直渐变 GradientStop { position: 0.0; color: "#ff0000" } GradientStop { position: 0.5; color: "#00ff00" } GradientStop { position: 1.0; color: "#0000ff" } } } }

QT5Style 版本(更灵活)

QT5Style.LinearGradient { anchors.fill: parent start: Qt.point(0, 0) // 起点 end: Qt.point(400, 400) // 终点 gradient: Gradient { GradientStop { position: 0.0; color: Qt.rgba(1, 0, 0, 1) } GradientStop { position: 0.5; color: Qt.rgba(0, 1, 0, 1) } GradientStop { position: 1.0; color: Qt.rgba(0, 0, 1, 1) } } }

五、ConicalGradient(锥形渐变)

坐标轴图示

270° ↑ | 180° ←---+--→ 0° / 360° | ↓ 90°

属性说明

属性说明示例
angle起始角度0=右,90=下,180=左,270=上
gradient颜色渐变从 angle 位置开始顺时针渐变

完整代码

QT5Style.ConicalGradient { anchors.fill: parent angle: 45 // 从右下45°方向开始 gradient: Gradient { GradientStop { position: 0.0; color: "white" } GradientStop { position: 1.0; color: "black" } } // 旋转动画 RotationAnimation on angle { from: 0 to: 360 duration: 2000 // 2秒转一圈 loops: Animation.Infinite } }

六、RadialGradient(径向渐变)

效果图示

中心点 ● ← 白色 (position: 0.0) /|\ / | \ / | \ ~~~~~~~~~ ← 渐变过渡 / \ ●───────────● ← 黑色 (position: 1.0) 水平半径: horizontalRadius 垂直半径: verticalRadius

属性说明

属性说明
horizontalRadius水平方向辐射半径
verticalRadius垂直方向辐射半径
angle旋转角度(配合椭圆使用)
gradient从中心向外的颜色渐变

完整代码

QT5Style.RadialGradient { anchors.fill: parent // 椭圆控制 horizontalRadius: 100 // 水平半径 verticalRadius: 300 // 垂直半径(椭圆效果) angle: 45 // 旋转45度 gradient: Gradient { GradientStop { position: 0.0; color: "white" } // 中心 GradientStop { position: 1.0; color: "black" } // 边缘 } }

常用效果

效果horizontalRadiusverticalRadius
正圆相同值相同值
横向椭圆较大较小
纵向椭圆较小较大

七、颜色格式

#AARRGGBB 格式

格式说明
#FF000000不透明黑色
#8000000050%透明黑色
#00000000完全透明
Qt.rgba(r, g, b, a)函数写法,a=0~1

八、快速选择指南

需要简单水平/垂直渐变? → Gradient 需要对角线或任意方向? → LinearGradient 需要旋转扫描效果? → ConicalGradient + RotationAnimation 需要光晕/球体/按钮? → RadialGradient

九、完整示例代码(可直接运行)

import QtQuick import QtQuick.Shapes 1.8 as QT6Style import Qt5Compat.GraphicalEffects as QT5Style Window { width: 640; height: 480 visible: true Rectangle { id: btn width: 400; height: 400 anchors.centerIn: parent // 切换下面四种效果查看 // === 1. 基础渐变 === // gradient: Gradient { // GradientStop { position: 0.0; color: "red" } // GradientStop { position: 1.0; color: "blue" } // } // === 2. 线性渐变 === // QT5Style.LinearGradient { // anchors.fill: parent // start: Qt.point(0, 0) // end: Qt.point(400, 400) // gradient: Gradient { // GradientStop { position: 0.0; color: "red" } // GradientStop { position: 1.0; color: "blue" } // } // } // === 3. 锥形渐变 === // QT5Style.ConicalGradient { // anchors.fill: parent // angle: 0 // gradient: Gradient { // GradientStop { position: 0.0; color: "white" } // GradientStop { position: 1.0; color: "black" } // } // RotationAnimation on angle { // from: 0; to: 360; duration: 2000; loops: Animation.Infinite // } // } // === 4. 径向渐变 === QT5Style.RadialGradient { anchors.fill: parent horizontalRadius: 200 verticalRadius: 200 gradient: Gradient { GradientStop { position: 0.0; color: "white" } GradientStop { position: 1.0; color: "black" } } } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 11:50:30

深蓝BREAKER:全球首家ORIVO认证南极磷虾油原料商,树立品质新标杆

近日,深蓝BREAKER(江苏深蓝生物科技有限公司)成功通过权威海洋脂质纯度验证机构——ORIVO 的认证,成为全球首家斩获该认证的南极磷虾油原料商,并获得其颁发的 100% 纯南极磷虾油证书,跻身全球极少数获此认证…

作者头像 李华
网站建设 2026/5/9 11:35:36

SystemC与SystemCrafter在DES加密硬件加速中的实践

1. SystemC与SystemCrafter在DES加密中的协同设计实践作为一名长期从事硬件加速开发的工程师,我亲历了从传统HDL到高层次综合(HLS)的技术演进。本文将分享如何利用SystemC和SystemCrafter SC工具链实现DES加密算法的硬件加速,这个…

作者头像 李华
网站建设 2026/5/9 11:33:30

基于改进YOLOv8斑点叉尾鮰鱼损伤检测系统的研究与实现

摘要:斑点叉尾鮰是我国重要的淡水养殖经济鱼类,在高密度集约化养殖过程中,鱼体损伤问题频发,直接影响商品鱼品质和养殖经济效益。传统的鱼体损伤检测主要依赖人工目视判别,存在效率低、主观性强、难以实现批量化检测等…

作者头像 李华
网站建设 2026/5/9 11:28:31

NHSE存档编辑工具:突破动物森友会限制的3大核心功能详解

NHSE存档编辑工具:突破动物森友会限制的3大核心功能详解 【免费下载链接】NHSE Animal Crossing: New Horizons save editor 项目地址: https://gitcode.com/gh_mirrors/nh/NHSE 你是否曾在《集合啦!动物森友会》中为收集稀有物品而耗费数周时间&…

作者头像 李华