qml实现自定义标题栏按钮
2024-10-19 00:22:47
自定义的标题栏按钮是由Rectangle来实现的,在Rectangle中需要4张图片,分别在鼠标进入按钮区、鼠标离开按钮区(正常状态下)、鼠标按下和鼠标释放时所加载的图片。下面是实现自定义按钮的代码(我把它放在了一个MaxButton.qml文件中):
Rectangle {
radius: 10 //设置圆角半径 property string normalPath //按钮正常和鼠标离开按钮区后的图片路径
property string enterPath //鼠标进入按钮区域时的图片路径
property string pressPath //鼠标按下时的图片路径
property string releasedPath //鼠标释放后的图片路径 signal buttonClick() //鼠标点击时发送此信号 Image {
id: background
anchors.fill: parent
source: normalPath
} MouseArea { //处理鼠标事件
anchors.fill: parent
hoverEnabled: true //处理没有按下时的鼠标事件
onClicked: buttonClick() //点击按钮时发送buttonClick信号
onEntered: background.source = enterPath //鼠标进入按钮区
onPressed: background.source = pressPath //鼠标按下
onExited: background.source = normalPath //鼠标离开按钮区
onReleased: background.source = releasedPath //鼠标释放
}
}
可以用它来实现标题栏中的关闭和最小化按钮。如果要想用它来实现最大化按钮还需要在他的父类中添加一些东西。因为窗口有两种状态,一种是正常大小,一种是最大窗口,所以要先在父类中判断窗口此时处在什么样的状态下,然后在将最大化按钮展现出不同的效果。以下是我在用它实现最大化按钮时在父类中的代码:
/**定义一个windowStatus属性来保存此时窗口的状态,
*等于true时说明窗口此时处于正常大小,
*等于false时说明窗口此时处于最大化状态
**/
property bool windowStatus: true MaxButton {//定义最大化按钮
id: maxButton
width: 35
height: 25 anchors.top: parent.top
anchors.right: closeButton.left //因为加载的窗口默认正常大小,所以初始化按钮时加载窗口处于正常状态下的图片
normalPath: "qrc:/image/全屏_2.png"
enterPath: "qrc:/image/全屏_3.png"
pressPath: "qrc:/image/全屏_1.png"
releasedPath: "qrc:/image/全屏_2.png" onButtonClick: maxButtonClicket()
}
function maxButtonClicket() {
if(windowStatus) {//检查此时窗口的状态
//windowStatus=true说明窗口此时处于正常大小 windowStatus = false //改变窗口状态 mainWindow.showMaximized() //将窗口最大化 //重新加载窗口最大化后的最大化按钮图标
maxButton.normalPath = "qrc:/image/窗口_2.png"
maxButton.enterPath = "qrc:/image/窗口_3.png"
maxButton.pressPath = "qrc:/image/窗口_1.png"
maxButton.releasedPath = "qrc:/image/窗口_2.png" }else { //windowStatus=false说明窗口此时处于最大化 windowStatus = true mainWindow.showNormal() //将窗口改为正常大小 maxButton.normalPath = "qrc:/image/全屏_2.png"
maxButton.enterPath = "qrc:/image/全屏_3.png"
maxButton.pressPath = "qrc:/image/全屏_1.png"
maxButton.releasedPath = "qrc:/image/全屏_2.png"
}
}
最新文章
- 优化MySchool数据库设计
- php面向对象之final的应用
- 安装xubuntu时遇到的一些问题
- C#基础之程序集(一)
- jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发
- 学渣也要搞 laravel(1)—— 安装篇
- ios开发之xcode6中如何添加pch全局引用文件
- Java Instanceof
- 10个工具让你的 shell 脚本更强大
- 10. Regular Expression Matching
- 动态样式语言—LESS
- int与integer的区别(基本数据类型与引用数据类型)
- centos6.8 静默安装 oracle 11.2.0.4
- php中如何给类规范的注释
- python字符串常用内置方法
- Java高并发--安全发布对象
- tomcat 改端口 运维最最重要的就是有看日志的习惯
- UVa 442 Matrix Chain Multiplication(栈的应用)
- django-ORM相关代码及其sql执行次数
- poj2387- Til the Cows Come Home(最短路)
热门文章
- web开发技术点解析
- javascript操作
- Mac OS 10.10 Yosemite正式版怎么升级 升级教程
- 分布式文件系统之GPFS
- Computer skills one can learn within one day
- PHP.1-网站开发概述
- 小团队开发管理工具:gitlab+redmine+testlink+jenkins
- flex数据交互方式 转
- 【XMLRPC实现跨语言编程】Tcl <;---->; python
- <;转>;HTML+CSS总结/深入理解CSS盒子模型