当前位置: 首页 > news >正文

Qt如何实现自定义标题栏

Qt 中实现自定义标题栏的关键是:
1️⃣ 先去掉系统默认标题栏和边框。
2️⃣ 使用自绘的 QWidget/QFrame 作为标题栏,自己实现拖动双击最大化最小化/关闭按钮等逻辑。

下面给你一个详细的实现步骤 + 可直接用的示例代码(Qt5/Qt6 通用):

完整示例下载地址:https://download.csdn.net/download/2501_91893428/91967952


1️⃣ 实现步骤

(1) 去掉系统默认标题栏

使用 Qt::FramelessWindowHint 去掉默认边框:

 
setWindowFlags(Qt::FramelessWindowHint | Qt::Window);

⚠️ 这样窗口没有系统边框,拖动、缩放、阴影都需要自己实现。

(2) 自定义标题栏

用一个 QWidget(或 QFrame)作为标题栏容器,可以放置:

  • 窗口标题文本QLabel

  • 最小化 / 最大化 / 关闭按钮QPushButtonQToolButton

(3) 实现拖动窗口

重写 mousePressEventmouseMoveEvent

  • 按下时记录鼠标位置

  • 移动时计算偏移量并调用 move() 移动窗口。

(4) 双击最大化/还原

重写 mouseDoubleClickEvent,双击标题栏切换 showMaximized() / showNormal()

(5) 自定义按钮功能

  • 最小化:showMinimized()

  • 最大化:isMaximized() ? showNormal() : showMaximized()

  • 关闭:close()


2️⃣ 完整示例(Qt Widgets)

MainWindow.h

 
#pragma once
#include <QWidget>
#include <QMouseEvent>class MainWindow : public QWidget
{Q_OBJECT
public:explicit MainWindow(QWidget *parent = nullptr);
protected:void mousePressEvent(QMouseEvent *event) override;void mouseMoveEvent(QMouseEvent *event) override;
private:QPoint m_dragPosition;
};

MainWindow.cpp

 
#include "MainWindow.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QApplication>MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{setWindowFlags(Qt::FramelessWindowHint | Qt::Window);   // 无边框setMinimumSize(600, 400);// ---- 自定义标题栏 ----QWidget *titleBar = new QWidget(this);titleBar->setFixedHeight(35);titleBar->setStyleSheet("background-color:#444; color:white;");QLabel *title = new QLabel("自定义标题栏窗口");QPushButton *btnMin = new QPushButton("-");QPushButton *btnMax = new QPushButton("□");QPushButton *btnClose = new QPushButton("×");for (auto btn : {btnMin, btnMax, btnClose}) {btn->setFixedSize(30, 30);btn->setStyleSheet("QPushButton {border:none; color:white;} ""QPushButton:hover {background:#666;}");}QHBoxLayout *titleLayout = new QHBoxLayout(titleBar);titleLayout->setContentsMargins(5, 0, 5, 0);titleLayout->addWidget(title);titleLayout->addStretch();titleLayout->addWidget(btnMin);titleLayout->addWidget(btnMax);titleLayout->addWidget(btnClose);// ---- 中心内容 ----QLabel *content = new QLabel("这里是主内容区域");content->setAlignment(Qt::AlignCenter);QVBoxLayout *mainLayout = new QVBoxLayout(this);mainLayout->setContentsMargins(1, 1, 1, 1);mainLayout->addWidget(titleBar);mainLayout->addWidget(content, 1);// ---- 按钮信号 ----connect(btnMin, &QPushButton::clicked, this, &QWidget::showMinimized);connect(btnMax, &QPushButton::clicked, [=] {isMaximized() ? showNormal() : showMaximized();});connect(btnClose, &QPushButton::clicked, this, &QWidget::close);
}void MainWindow::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton) {m_dragPosition = event->globalPos() - frameGeometry().topLeft();event->accept();}
}void MainWindow::mouseMoveEvent(QMouseEvent *event)
{if (event->buttons() & Qt::LeftButton) {move(event->globalPos() - m_dragPosition);event->accept();}
}

main.cpp

 
#include "MainWindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

3️⃣ 进阶功能

  • 窗口阴影

    • Windows:使用 QGraphicsDropShadowEffect

    • Mac/Linux:可用 Qt::FramelessWindowHint + setAttribute(Qt::WA_TranslucentBackground)

  • 可拉伸调整大小

    • 需检测鼠标靠近边框的区域,自行实现拉伸逻辑。

  • 动画效果

    • 使用 QPropertyAnimation 实现平滑最小化/最大化。


总结

 完整示例下载地址:https://download.csdn.net/download/2501_91893428/91967952 

✅ 核心:去系统边框 + 自绘标题栏 + 处理拖动/按钮事件
✅ 优点:外观完全自定义,适合现代UI
✅ 缺点:需自行实现阴影、缩放、系统菜单等功能

http://www.dtcms.com/a/391420.html

相关文章:

  • Qt QPlugin界面插件式开发Q_DECLARE_INTERFACE、Q_PLUGIN_METADATA和Q_INTERFACES
  • 梯度增强算法(Gradient Boosting)学习笔记
  • 确保邵氏硬度计测量精度问题要考虑事宜
  • `scroll-margin-top`控制当页面滚动到某个元素滚时,它在视口预留的位置,上方留白
  • 内存管理-伙伴系统合并块计算,__find_buddy_pfn,谁是我的伙伴???
  • 【LVS入门宝典】LVS核心原理与实战:Director(负载均衡器)配置指南
  • 算法常考题:描述假设检验的过程
  • IEEE会议征集分论坛|2025年算法、软件与网络安全国际学术会议(ASNS2025)
  • 洞见未来:计算机视觉的发展、机遇与挑战
  • MongoDB集合学习笔记
  • C++ 中 std::list使用详解和实战示例
  • IO流的简单介绍
  • 【AI论文】SAIL-VL2技术报告
  • 基于 SSM(Spring+SpingMVC+Mybatis)+MySQL 实现(Web)软件测试用例在线评判系统
  • 【2/20】理解 JavaScript 框架的作用:Vue 在用户界面中的应用,实现一个动态表单应用
  • Android冷启动和热启动以及温启动都是什么意思
  • Java数据结构 - 单链表的模拟实现
  • git忽略CRLF警告
  • 不用手也能玩手机?多代理协作框架让 APP 自动执行任务
  • 装备制造企业支撑智能制造的全生命周期数据治理实践
  • 【论文阅读 | AAAI 2025 | Mamba YOLO: 基于状态空间模型的目标检测简单基线​​】
  • AdMergeX与小川科技最右App合作案例入选中国信通院“高质量数字化转型典型案例集”
  • LoadBalancer配置
  • 国内外主流开源密码库:演进背景、国密适配与企业维护挑战
  • 车规级MCU在特种车辆车身控制中的应用研究
  • 深度学习基本模块:GRU 门控循环单元
  • 通过Keepalived+LVS搭建NAT模式的高可用集群系统保姆级教程
  • 设备硬件能力调用:传感器、蓝牙与定位
  • 完全二叉树的链式创建以及遍历
  • 数据结构——二叉树和BST(2)