27、Qt自定义标题栏

一、说明

QtWidget及其子类有默认的标题栏,但是这个标题栏不能美化,有时候满足不了我们的使用需求,所以进行自定义标题栏

二、下载图标

在下面的链接中下载两种颜色的最大化、向下还原、最大化和关闭八个图片,并找一张当做图标的图片

iconfont-阿里巴巴矢量图标库

 

三、新建项目

新建Qt项目

更改项目名称和位置

选择编译器

基类选择QWidget;

默认

把刚才下载的图片拷贝到工程目录下

右击项目名称,选择“Add New”

选择如下模板

输入名称

默认

选择添加、添加前缀

清空前缀中的原有内容

选择添加、添加文件

选择拷贝过来的所以图片

Ctrl+S保存

四、创建自定义类

选择“C++ Class"模板

基类选择QWidget

默认

更改mytitlebar.h中的代码

#ifndef MYTITLEBAR_H
#define MYTITLEBAR_H#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QPushButton>class MyTitleBar : public QWidget
{Q_OBJECT
public:explicit MyTitleBar(QWidget *parent = nullptr);void setColor(QString backgroundColor, QString fontColor, QString selectedFontColor); //设置颜色
protected://界面拖动void mousePressEvent(QMouseEvent* event); //鼠标按下void mouseMoveEvent(QMouseEvent* event); //鼠标移动void mouseReleaseEvent(QMouseEvent* event); //鼠标抬起void mouseDoubleClickEvent(QMouseEvent* event); //双击标题栏进行界面的最大化/还原bool eventFilter(QObject *obj, QEvent *event); //事件过滤器private slots:void onClicked(); //进行最小化、最大化/还原、关闭操作void updateMaximize(); //最大化/还原private:QLabel* m_iconLabel; //显示图标QLabel* m_titleLabel; //显示标题QPushButton* m_minimizeButton; //最小化按键QPushButton* m_maximizeButton; //最大化/还原按键QPushButton* m_closeButton; //关闭按键QPoint m_mousePosition; //鼠标按下时的位置bool m_isMousePressed; //鼠标是否摁下
};#endif // MYTITLEBAR_H

更改mytitlebar.cpp中的代码

#include "mytitlebar.h"
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>MyTitleBar::MyTitleBar(QWidget *parent) : QWidget(parent)
{setFixedHeight(30);//控件初始化m_iconLabel = new QLabel(this);m_titleLabel = new QLabel(this);m_minimizeButton = new QPushButton(this);m_maximizeButton = new QPushButton(this);m_closeButton = new QPushButton(this);//图片自适应控件大小m_iconLabel->setFixedSize(20, 20);m_iconLabel->setScaledContents(true);//设置控件在布局(layout)里面的大小变化的属性m_titleLabel->setMinimumHeight(25);m_titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);m_titleLabel->setAlignment(Qt::AlignCenter); //居中//设置控件的最小大小和最大大小m_minimizeButton->setFixedSize(30, 25);m_maximizeButton->setFixedSize(30, 25);m_closeButton->setFixedSize(30, 25);//设置控件唯一标识符m_titleLabel->setObjectName("whiteLabel");m_minimizeButton->setObjectName("minimizeButton");m_maximizeButton->setObjectName("maximizeButton");m_closeButton->setObjectName("closeButton");m_minimizeButton->setIcon(QIcon(":/icon/minWhite.png"));m_minimizeButton->setToolTip(tr("最小化"));m_minimizeButton->installEventFilter(this);m_maximizeButton->setIcon(QIcon(":/icon/restoreWhite.png"));m_maximizeButton->setToolTip(tr("向下还原"));m_maximizeButton->installEventFilter(this);m_closeButton->setIcon(QIcon(":/icon/closeWhite.png"));m_closeButton->setToolTip(tr("关闭"));m_closeButton->installEventFilter(this);//按键背景透明,图标颜色为m_fontColorm_titleLabel->setStyleSheet(QString("color:white;"));m_minimizeButton->setStyleSheet(QString("background-color:rgba(0,0,0,0);"));m_maximizeButton->setStyleSheet(QString("background-color:rgba(0,0,0,0);"));m_closeButton->setStyleSheet(QString("background-color:rgba(0,0,0,0);"));//控件布局QHBoxLayout *pLayout = new QHBoxLayout(this);pLayout->addWidget(m_iconLabel);pLayout->addSpacing(5);pLayout->addWidget(m_titleLabel);pLayout->addWidget(m_minimizeButton);pLayout->addWidget(m_maximizeButton);pLayout->addWidget(m_closeButton);pLayout->setSpacing(0);setLayout(pLayout);//信号槽绑定connect(m_minimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));connect(m_maximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
}/**
* @brief MainWindow::mousePressEvent 鼠标双击:界面最大化或还原
* @param event 鼠标事件
*/
void MyTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{Q_UNUSED(event);emit m_maximizeButton->clicked();
}/**
* @brief MainWindow::mousePressEvent 鼠标按下:准备移动
* @param event 鼠标事件
*/
void MyTitleBar::mousePressEvent(QMouseEvent *event)
{m_mousePosition = event->pos(); //鼠标在控件中的位置m_isMousePressed = true;
}/**
* @brief MainWindow::mousePressEvent 鼠标移动
* @param event 鼠标事件
*/
void MyTitleBar::mouseMoveEvent(QMouseEvent *event)
{if(m_isMousePressed == true ){QWidget *pWindow = this->window();if(pWindow->isMaximized()) //界面最大时,先还原再移动{pWindow->showNormal();//防止鼠标指针在界面之外m_mousePosition = QPoint(200, 10);pWindow->move(event->globalPos().x() - 200, event->globalPos().y());}else{QPoint movePot = event->globalPos() - m_mousePosition;pWindow->move(movePot);}}
}/**
* @brief MainWindow::mousePressEvent 鼠标抬起:移动结束
* @param event 鼠标事件
*/
void MyTitleBar::mouseReleaseEvent(QMouseEvent *event)
{Q_UNUSED(event);m_isMousePressed = false;
}/**
* @brief MyTitleBar::eventFilter 事件过滤器
* @param obj 过滤对象
* @param event 事件
* @return
*/
bool MyTitleBar::eventFilter(QObject *obj, QEvent *event)
{switch(event->type()){case QEvent::WindowTitleChange: //更改标题显示内容{QWidget *pWidget = qobject_cast<QWidget *>(obj);if(pWidget){m_titleLabel->setText(pWidget->windowTitle());}}break;case QEvent::WindowIconChange: //更改图标{QWidget *pWidget = qobject_cast<QWidget *>(obj);if(pWidget){QIcon icon = pWidget->windowIcon();m_iconLabel->setPixmap(icon.pixmap(m_iconLabel->size()));}}break;case QEvent::WindowStateChange:case QEvent::Resize:updateMaximize(); //最大化/还原break;case QEvent::Enter: //鼠标悬停在控件上,控件变色{if(obj == m_minimizeButton){m_minimizeButton->setIcon(QIcon(":/icon/minRed.png"));}else if(obj == m_closeButton){m_closeButton->setIcon(QIcon(":/icon/closeRed.png"));}else if(obj == m_maximizeButton){if(m_maximizeButton->property("maximizeProperty") == "向下还原"){m_maximizeButton->setIcon(QIcon(":/icon/restoreRed.png"));}else{m_maximizeButton->setIcon(QIcon(":/icon/maxRed.png"));}}}break;case QEvent::Leave: //鼠标离开控件,颜色恢复{if(obj == m_minimizeButton){m_minimizeButton->setIcon(QIcon(":/icon/minWhite.png"));}else if(obj == m_closeButton){m_closeButton->setIcon(QIcon(":/icon/closeWhite.png"));}else if(obj == m_maximizeButton){if(m_maximizeButton->property("maximizeProperty") == "向下还原"){m_maximizeButton->setIcon(QIcon(":/icon/restoreWhite.png"));}else{m_maximizeButton->setIcon(QIcon(":/icon/maxWhite.png"));}}}break;default:break;}return QWidget::eventFilter(obj, event);
}/**
* @brief MyTitleBar::onClicked 进行最小化、最大化/还原、关闭操作
*/
void MyTitleBar::onClicked()
{QPushButton *pButton = qobject_cast<QPushButton *>(sender());QWidget *pWindow = this->window();if(pWindow->isTopLevel()){if(pButton == m_minimizeButton) //最小化{pWindow->showMinimized();}else if (pButton == m_maximizeButton) //最大化、还原{pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();}else if (pButton == m_closeButton) //关闭{pWindow->close();}}
}/**
* @brief MyTitleBar::updateMaximize 最大化/还原时,更改图标样式
*/
void MyTitleBar::updateMaximize()
{QWidget *pWindow = this->window();if(pWindow->isTopLevel()){if(pWindow->isMaximized()){m_maximizeButton->setToolTip(tr("向下还原"));m_maximizeButton->setProperty("maximizeProperty", "向下还原");m_maximizeButton->setIcon(QIcon(":/icon/restoreWhite.png"));}else{m_maximizeButton->setProperty("maximizeProperty", "最大化");m_maximizeButton->setToolTip(tr("最大化"));m_maximizeButton->setIcon(QIcon(":/icon/maxWhite.png"));}m_maximizeButton->setStyle(QApplication::style());}
}

更改widget.h中的代码

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "mytitlebar.h"namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = nullptr);~Widget();protected:void resizeEvent(QResizeEvent *event);private:Ui::Widget *ui;MyTitleBar* m_titleBar; //自定义标题栏
};#endif // WIDGET_H

更改widget.cpp中的代码

#include "widget.h"
#include "ui_widget.h"
#include <QLayout>Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{ui->setupUi(this);//第一个是去掉原边框及标题栏,第二个是保留最小化及还原功能,主要是为了还原,最小化下面实现了this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);QVBoxLayout *layout = new QVBoxLayout;//自定义标题栏m_titleBar = new MyTitleBar;m_titleBar->resize(this->width(), 30);installEventFilter(m_titleBar);setWindowTitle("自定义标题栏"); //设置标题内容setWindowIcon(QIcon(":/icon/capricorn.png")); //设置图标QWidget *centerWidget = new QWidget; //中间窗口layout->addWidget(m_titleBar);layout->addWidget(centerWidget);layout->setContentsMargins(5, 0, 5, 2); //设置左侧、右侧边距为5this->setLayout(layout);this->showMaximized(); //最大化显示QString backgroundColor = "black"; //背景色QString fontColor = "white"; //字体色//渐变背景this->setStyleSheet(QString("QWidget#Widget{background-color:qlineargradient(x1:0,     y1:0, x2:1, y2:1, stop:0 %1, stop:1 %2);}")
.arg(backgroundColor).arg(fontColor));
}Widget::~Widget()
{delete ui;
}/**
* @brief MainWindow::resizeEvent 当界面大小改变时更改标题栏大小
* @param event
*/
void Widget::resizeEvent(QResizeEvent *event)
{Q_UNUSED(event);m_titleBar->resize(this->width(), 30); //更改标题栏大小
}

五、运行测试

可以进行放大缩小移动等

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

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

相关文章

【ITK配准】第二十一期 Demons变形配准

很高兴在雪易的CSDN遇见你 VTK技术爱好者 QQ:870202403 公众号:VTK忠粉 前言 本文分享ITK配准中Demons变形配准,希望对各位小伙伴有所帮助! 感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步! 你的点赞就是我的动力(^U^)ノ~YO 目录 前言 Demons…

Spring 框架中用到的设计模式

概述 1.工厂模式:BeanFactory 2.代理模式 AOP动态代理 3.单例模式:spring中bean都是单例模式&#xff0c;bean默认都是单例的 4.模板方法:postProcessorBeanFactory, onRefresh&#xff0c;initpropertyValue 5.观察者模式:listener,event,multicast 6.适配器模式:Adapter 7.装…

shell基础知识

一.Shell概述 Shell是一个命令行解释器,它接收用户命令,然后调用操作系统内核 二.Shell解析器 cat /etc/shells echo $SHELL 三.Shell脚本入门 #!/bin/bash bash helloworld.sh chmod 777 helloworld.sh 四.Shell中的变量 echo $HOME | $PWD | $SHELL | $USER set 显示当前Shel…

使用GitLab自带的CI/CD功能在K8S集群里部署项目(四)

前置内容&#xff1a; 通过Docker Compose部署GitLab和GitLab Runner&#xff08;一&#xff09; 使用GitLab自带的CI/CD功能在本地部署项目&#xff08;二&#xff09; 使用GitLab自带的CI/CD功能在远程服务器部署项目&#xff08;三&#xff09; 一、K8S集群信息 节点名称…

基于springboot+vue+Mysql的体质测试数据分析及可视化设计

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

JAVA反射示例

public static void main(String[] args) throws Exception {Class pClass Class.forName("jdj.Person");//遍历整个控制器Constructor[] constructorspClass.getDeclaredConstructors();for(Constructor con:constructors) {System.out.println(con);}//通过控制器…

【CCF-CSP】202403-3 化学方程式配平

输入格式&#xff1a; 从标准输入读入数据。 输入的第一行包含一个正整数 n&#xff0c;表示需要判断的化学方程式的个数。 接下来的 n 行&#xff0c;每行描述了一个需要被配平的化学方程式。包含空格分隔的一个正整数和全部涉及物质的化学式。其中&#xff0c;正整数 m 表…

Ubuntu 安装 samba 实现文件共享

1. samba的安装: sudo apt-get install samba sudo apt-get install smbfs2. 创建共享目录 mkdir /home/share sudo chmod -R 777 /home/share3. 创建Samba配置文件: 3.1 保存现有的配置文件 sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak3.2 打开现有的文件 sudo…

Docker快速启动清单

以下容器均使用 Docker version 24.0.2 版本测试使用&#xff0c;这里需要注意一下&#xff0c;高版本的Docker不支持镜像V1版本&#xff0c;不知道怎么操作才可以让它支持&#xff0c;所以推荐使用低版本 如果觉得不直观&#xff0c;或者觉得有点乱&#xff0c;可以访问以下网…

第十二章 元数据管理练习

单选题 (每题1分,共23道题) 1、 [单选] 元数据的来源有哪些? A:参考数据库、BI工具 B:数据字典、数据集成工具 C:建模工具和事件消息工具 D:以上都是 正确答案:D 你的答案:D 解析:327~330页,一共15个来源,所以选D,考核知识点,元数据的基本概念,元数据来源相关…

Window如何运行sh文件以及wget指令

Git下载 官网链接如下&#xff1a;https://gitforwindows.org/ 安装就保持一路无脑安装就行&#xff0c;不需要改变安装过程中的任何一个选项。 配置Git 切刀桌面&#xff0c;随便右击屏幕空白处&#xff0c;点open Git Bash here 把这行复制过去&#xff0c;回车&#xff1…

【用文本生成歌声】Learn2Sing 2.0——歌声转换算法及梅尔频谱详解

一. 频谱图与梅尔谱图的介绍 频谱图&#xff1a;频谱图可以理解为一堆垂直堆叠在一起的快速傅里叶变换结果。 1.1 信号 在进入频谱图模块之前&#xff0c;首先我们需要了解信号是什么。 信号就是某一特定量随时间变化&#xff0c;对于音频来说&#xff0c;这个特定的变化量就…

Python图形复刻——绘制母亲节花束

各位小伙伴&#xff0c;好久不见&#xff0c;今天学习用Python绘制花束。 有一种爱&#xff0c;不求回报&#xff0c;有一种情&#xff0c;无私奉献&#xff0c;这就是母爱。祝天下妈妈节日快乐&#xff0c;幸福永远&#xff01; 图形展示&#xff1a; 代码展示&#xff1a; …

多目标跟踪入门介绍

多目标跟踪算法 我们也可以称之为 Multi-Target-Tracking &#xff08;MTT&#xff09;。 那么多目标跟踪是什么&#xff1f; 不难看出&#xff0c;跟踪算法同时会为每个目标分配一个特定的 id 。 由此得出了目标跟踪与目标检测的区别&#xff08;似乎都是用方框来框出目标捏…

创新案例|搜索新王Perplexity如何构建生成式AI产品开发的新模式

Perplexity AI&#xff1a;生成式搜索的颠覆者 刚刚成立满两年&#xff0c;Perplexity AI已经变成了我日常频繁使用的工具&#xff0c;甚至取代了我对 Google搜索的依赖 —— 而我并非个案。该公司仅凭不到 50 名员工&#xff0c;已经吸引了数千万用户。他们目前的年收入超过 …

数据赋能(81)——数据要素:管理必要性

数据作为现代社会的核心资源&#xff0c;其潜在价值巨大。有效的数据要素管理能够确保数据得到充分的利用&#xff0c;转化为具备潜在利用价值的数据资产&#xff0c;为使用者或所有者带来经济效益。数据要素管理涉及到数据的收集、存储、处理、分析和利用等各个环节&#xff0…

马尔可夫决策过程(Markov Decision Process,简称MDP)

马尔可夫决策过程是一个四元组&#xff08;S, A, P, R&#xff09;&#xff0c;其中&#xff1a; S是状态集合&#xff0c;表示智能体&#xff08;Agent&#xff09;可能处于的所有状态&#xff1b;A是动作集合&#xff0c;表示智能体可以采取的所有动作&#xff1b;P是状态转…

Linux 操作系统线程1

目录 一、线程 1.1线程的基本概念 1.2 线程相关的API函数 1.2.1 线程的创建 1.2.2 线程退出 1.2.3 线程等待函数 1.2.4 获取线程ID 1.2.5 线程取消 1.2.6 线程的清理函数 一、线程 1.1线程的基本概念 线程是属于进程&#xff1b;一个进程可以有多个线程&#xff…

第五十八节 Java设计模式 - 适配器模式

Java设计模式 - 适配器模式 我们在现实生活中使用适配器很多。例如&#xff0c;我们使用存储卡适配器连接存储卡和计算机&#xff0c;因为计算机仅支持一种类型的存储卡&#xff0c;并且我们的卡与计算机不兼容。 适配器是两个不兼容实体之间的转换器。适配器模式是一种结构模…

一文看懂深度学习中的cuda环境配置:cuda,cuda driver,cudnn与pytorch-cuda

深度学习中通常会涉及到cuda环境相关的问题&#xff0c;特别是torch版本&#xff0c;cuda版本等兼容问题。 主要涉及到这四个方向 显卡驱动&#xff1a;cuda driver 。驱动API &#xff08;driver API&#xff09;通过nvidia-smi查看&#xff0c;是所有cuda环境的基础CudaTool…