
QML按钮渐变及多种状态实现(附完整工程)
发布日期:2021-05-13 21:00:44
浏览次数:28
分类:精选文章
本文共 2373 字,大约阅读时间需要 7 分钟。
QButton按钮效果展示与实现
本文将详细介绍QML(Qt Quick)中自定义按钮控件QButton的实现效果及其实现方法
按钮渐变效果
QButton控件支持不同状态下的渐变背景效果,具体实现如下
Rectangle { id: leave gradient: Gradient { GradientStop { position: 0.0; color: "#787878" } GradientStop { position: 0.495; color: "#000000" } GradientStop { position: 0.495; color: "#000000" } GradientStop { position: 1.0; color: "#787878" } }}Rectangle { id: hover gradient: Gradient { GradientStop { position: 0.0; color: "#AAAAAA" } GradientStop { position: 0.495; color: "#000000" } GradientStop { position: 0.495; color: "#000000" } GradientStop { position: 1.0; color: "#AAAAAA" } }}
按钮状态交互
通过MouseArea组件实现按钮的按压、悬停、释放等状态交互
MouseArea { anchors.fill: parent hoverEnabled: true onPressed: { parent.gradient = leave.gradient root.pressed() //文字下沉效果 buttonText1.x = buttonText1.x + 1 buttonText1.y = buttonText1.y + 1 } onEntered: { parent.gradient = hover.gradient root.hovered() } onExited: { parent.gradient = leave.gradient } onReleased: { parent.gradient = hover.gradient root.released() //文字恢复效果 buttonText1.x = buttonText1.x - 1 buttonText1.y = buttonText1.y - 1 } onClicked: { root.clicked() }}
文字下沉效果实现
通过在按钮按压时移动文字位置,实现文字下沉的视觉效果
Text { id: buttonText text: "Button" visible: false anchors.centerIn: parent color: "white" font.pointSize: 15}Text { id: buttonText1 text: buttonText.text x: buttonText.x y: buttonText.y color: buttonText.color font.pointSize: buttonText.font.pointSize}
按钮在主.qml中的使用
主.qml中使用QButton控件的示例代码如下
import QtQuick 2.12import QtQuick.Window 2.12Window { visible: true width: 480 height: 240 title: qsTr("Hello QButton") QButton { id: btn1 x: 100 y: 100 textSize: 15 onClicked: console.log("---------被点击") onPressed: console.log("---被按下") onHovered: console.log("*****指针来了*****") } QButton { anchors.left: btn1.right anchors.top: btn1.top anchors.leftMargin: 4 text: "胡皓天" width: 140 height: 40 }}
项目展示
通过以上实现,可在按钮按压时显示文字下沉效果,在释放时恢复原状,完美呈现按钮交互的视觉反馈
如果需要更详细的技术实现和优化,可以参考完整的QButton.qml和main.qml代码文件
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月11日 18时31分06秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python IO编程
2019-03-06
CSS入门总结
2019-03-06
使用 TortoiseGit 时,报 Access denied 错误
2019-03-06
基于 HTML5 WebGL 的污水处理厂泵站自控系统
2019-03-06
[系列] Go gRPC 调试工具
2019-03-06
django-表单之模型表单渲染(六)
2019-03-06
c++之程序流程控制
2019-03-06
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
2019-03-06
httprunner学习23-加解密
2019-03-06
有道云笔记 同步到我的博客园
2019-03-06
李笑来必读书籍整理
2019-03-06
http头部 Expect
2019-03-06
Hadoop(十六)之使用Combiner优化MapReduce
2019-03-06
《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》
2019-03-06
CoreCLR源码探索(八) JIT的工作原理(详解篇)
2019-03-06
IOS开发Swift笔记16-错误处理
2019-03-07
flume使用中的一些常见错误解决办法 (地址已经使用)
2019-03-07
andriod 开发错误记录
2019-03-07
C语言编译错误列表
2019-03-07