Qt 无边框窗口自定义标题栏

2024-04-26 09:18

本文主要是介绍Qt 无边框窗口自定义标题栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现了自定义标题栏的最小化,最大化(自适应任务栏),关闭,拉伸,拖曳。

因为不想引入其他资源,所以按钮,背景都是系统提供的。

效果图:



直接上代码。

用法:

#include "widget.h"
#include <QApplication>
#include "CustomFrame.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget *w = new Widget();CustomFrame frame(w, "CustomFrame");frame.show();return a.exec();
}

#pragma once#include <QFrame>class QToolButton;
class CustomFrame : public QFrame
{Q_OBJECT
public:explicit CustomFrame(QWidget *contentWidget, const QString &title);public slots:void slotShowSmall();void slotShowMaxRestore();protected:void mousePressEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);bool nativeEvent(const QByteArray & eventType, void * message, long * result);void paintEvent(QPaintEvent *);private:bool isMax_;bool isPress_;QPoint startPos_;QPoint clickPos_;QWidget *contentWidget_;QPixmap maxPixmap_;QPixmap restorePixmap_;QToolButton *maxButton_;
};


#include "CustomFrame.h"
#include <QtWidgets>static const int TITLE_HEIGHT = 30;
static const int FRAME_BORDER = 2;
CustomFrame::CustomFrame(QWidget *contentWidget, const QString &title): contentWidget_(contentWidget)
{this->setWindowFlags(Qt::FramelessWindowHint);this->setAttribute(Qt::WA_TranslucentBackground);this->setMouseTracking(true);isMax_ = false;isPress_ = false;QLabel *logoLabel = new QLabel();QPixmap logoPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMenuButton);logoLabel->setPixmap(logoPixmap);logoLabel->setFixedSize(16, 16);logoLabel->setScaledContents(true);QLabel *titleLabel = new QLabel();titleLabel->setText(title);QFont titleFont = titleLabel->font();titleFont.setBold(true);titleLabel->setFont(titleFont);titleLabel->setObjectName("whiteLabel");QToolButton *minButton = new QToolButton();QPixmap minPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMinButton);minButton->setIcon(minPixmap);connect(minButton, SIGNAL(clicked()), this, SLOT(slotShowSmall()));maxButton_ = new QToolButton();maxPixmap_ = this->style()->standardPixmap(QStyle::SP_TitleBarMaxButton);restorePixmap_ = this->style()->standardPixmap(QStyle::SP_TitleBarNormalButton);maxButton_->setIcon(maxPixmap_);connect(maxButton_, SIGNAL(clicked()), this, SLOT(slotShowMaxRestore()));QToolButton *closeButton = new QToolButton();QPixmap closePixmap = this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton);closeButton->setIcon(closePixmap);connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));QHBoxLayout *titleLayout = new QHBoxLayout();titleLayout->addWidget(logoLabel);titleLayout->addWidget(titleLabel);titleLabel->setContentsMargins(5, 0, 0, 0);titleLayout->addStretch();titleLayout->addWidget(minButton, 0, Qt::AlignTop);titleLayout->addWidget(maxButton_, 0, Qt::AlignTop);titleLayout->addWidget(closeButton, 0, Qt::AlignTop);titleLayout->setSpacing(0);titleLayout->setContentsMargins(5, 0, 0, 0);QWidget *titleWidget = new QWidget();titleWidget->setLayout(titleLayout);titleWidget->installEventFilter(0);QVBoxLayout *mainLayout = new QVBoxLayout();mainLayout->addWidget(titleWidget);mainLayout->addWidget(contentWidget_);mainLayout->setSpacing(0);mainLayout->setMargin(5);this->setLayout(mainLayout);
}void CustomFrame::slotShowSmall()
{this->showMinimized();
}void CustomFrame::slotShowMaxRestore()
{if (isMax_) {this->showNormal();maxButton_->setIcon(maxPixmap_);} else {this->showMaximized();maxButton_->setIcon(restorePixmap_);}isMax_ = !isMax_;
}void CustomFrame::mousePressEvent(QMouseEvent *e)
{startPos_ = e->globalPos();clickPos_ = e->pos();if (e->button() == Qt::LeftButton) {if (e->type() == QEvent::MouseButtonPress) {isPress_ = true;} else if (e->type() == QEvent::MouseButtonDblClick && e->pos().y() <= TITLE_HEIGHT) {this->slotShowMaxRestore();}}
}void CustomFrame::mouseMoveEvent(QMouseEvent *e)
{if (isMax_ || !isPress_) {return;}this->move(e->globalPos() - clickPos_);
}void CustomFrame::mouseReleaseEvent(QMouseEvent *)
{isPress_ = false;
}bool CustomFrame::nativeEvent(const QByteArray & eventType, void * message, long * result)
{Q_UNUSED(eventType);const int HIT_BORDER = 5;const MSG *msg=static_cast<MSG*>(message);if(msg->message == WM_NCHITTEST) {int xPos = ((int)(short)LOWORD(msg->lParam)) - this->frameGeometry().x();int yPos = ((int)(short)HIWORD(msg->lParam)) - this->frameGeometry().y();if(this->childAt(xPos,yPos) == 0) {*result = HTCAPTION;} else {return false;}if(xPos > 0 && xPos < HIT_BORDER) {*result = HTLEFT;}if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0)) {*result = HTRIGHT;}if(yPos > 0 && yPos < HIT_BORDER) {*result = HTTOP;}if(yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {*result = HTBOTTOM;}if(xPos > 0 && xPos < HIT_BORDER && yPos > 0 && yPos < HIT_BORDER) {*result = HTTOPLEFT;}if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > 0 && yPos < HIT_BORDER) {*result = HTTOPRIGHT;}if(xPos > 0 && xPos < HIT_BORDER && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {*result = HTBOTTOMLEFT;}if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {*result = HTBOTTOMRIGHT;}return true;}return false;
}void CustomFrame::paintEvent(QPaintEvent *e)
{int border = FRAME_BORDER;if (this->isMaximized()) {border = 0;}QPainter painter(this);QPainterPath painterPath;painterPath.setFillRule(Qt::WindingFill);painterPath.addRect(border, border, this->width()-2*border, this->height()-2*border);painter.setRenderHint(QPainter::Antialiasing, true);painter.fillPath(painterPath, QBrush(Qt::white));QColor color(200, 200, 200);for (int i=0; i<border; i++) {color.setAlpha((i+1)*30);painter.setPen(color);painter.drawRect(border-i, border-i, this->width()-(border-i)*2, this->height()-(border-i)*2);}painter.setPen(Qt::NoPen);painter.setBrush(Qt::white);// 这里可以在资源中指定一张标题背景图片//painter.drawPixmap(QRect(border, border, this->width()-2*border, this->height()-2*border), QPixmap(DEFAULT_SKIN));painter.drawRect(QRect(border, TITLE_HEIGHT, this->width()-2*border, this->height()-TITLE_HEIGHT-border));QFrame::paintEvent(e);
}


这篇关于Qt 无边框窗口自定义标题栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/937181

相关文章

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义