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

相关文章

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

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

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

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

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定