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

相关文章

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

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.引言