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

QT无边框窗口

1. 创建无边框窗口

通过设置窗口标志来去掉边框。

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtCore/Qt>class MainWindow : public QMainWindow {
public:MainWindow() {// 去掉边框setWindowFlags(Qt::FramelessWindowHint);// 设置窗口大小resize(800, 600);}
};

2. 创建自定义标题栏

创建一个继承自 QWidget 的类,包含按钮和标题。

#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtCore/Qt>class CustomTitleBar : public QWidget {
public:CustomTitleBar(QWidget* parent = nullptr) : QWidget(parent) {setupUI();}private:void setupUI() {QHBoxLayout* layout = new QHBoxLayout(this);layout->setContentsMargins(0, 0, 0, 0);// 标题QLabel* title = new QLabel(parent()->windowTitle());layout->addWidget(title);// 按钮QPushButton* minBtn = new QPushButton("最小化");QPushButton* maxBtn = new QPushButton("最大化");QPushButton* closeBtn = new QPushButton("关闭");layout->addWidget(minBtn);layout->addWidget(maxBtn);layout->addWidget(closeBtn);// 信号连接connect(minBtn, &QPushButton::clicked, parent(), &QWidget::showMinimized);connect(maxBtn, &QPushButton::clicked, parent(), &QWidget::showMaximized);connect(closeBtn, &QPushButton::clicked, parent(), &QWidget::close);}
};

3. 处理窗口移动

重写鼠标事件,实现拖动窗口。

#include <QtWidgets/QMainWindow>
#include <QtCore/QPoint>
#include <QtCore/QMouseEvent>class MainWindow : public QMainWindow {
public:MainWindow() {setWindowFlags(Qt::FramelessWindowHint);resize(800, 600);// 添加自定义标题栏CustomTitleBar* titleBar = new CustomTitleBar(this);setMenuWidget(titleBar); // 将标题栏设置为菜单栏}protected:bool dragging = false;QPoint dragPos;void mousePressEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = true;dragPos = event->globalPos() - pos();event->accept();}}void mouseMoveEvent(QMouseEvent* event) override {if (dragging) {move(event->globalPos() - dragPos);event->accept();}}void mouseReleaseEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = false;}}
};

4. 处理窗口调整大小

重写调整大小事件,确保标题栏正确显示。

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtCore/QEvent>class MainWindow : public QMainWindow {
public:MainWindow() {setWindowFlags(Qt::FramelessWindowHint);resize(800, 600);CustomTitleBar* titleBar = new CustomTitleBar(this);setMenuWidget(titleBar);}protected:void resizeEvent(QResizeEvent* event) override {QMainWindow::resizeEvent(event);// 确保标题栏的大小与窗口一致if (menuWidget()) {menuWidget()->setGeometry(QRect(0, 0, width(), menuWidget()->height()));}}
};

5. 复用标题栏

将标题栏类作为独立组件,多个窗口可复用。

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtCore/Qt>class AnotherWindow : public QMainWindow {
public:AnotherWindow() {setWindowFlags(Qt::FramelessWindowHint);resize(400, 300);CustomTitleBar* titleBar = new CustomTitleBar(this);setMenuWidget(titleBar);}protected:bool dragging = false;QPoint dragPos;void mousePressEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = true;dragPos = event->globalPos() - pos();event->accept();}}void mouseMoveEvent(QMouseEvent* event) override {if (dragging) {move(event->globalPos() - dragPos);event->accept();}}void mouseReleaseEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = false;}}void resizeEvent(QResizeEvent* event) override {QMainWindow::resizeEvent(event);if (menuWidget()) {menuWidget()->setGeometry(QRect(0, 0, width(), menuWidget()->height()));}}
};

6. 完整示例代码

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtCore/Qt>
#include <QtCore/QPoint>
#include <QtCore/QMouseEvent>
#include <QtCore/QResizeEvent>// 自定义标题栏类
class CustomTitleBar : public QWidget {
public:CustomTitleBar(QWidget* parent = nullptr) : QWidget(parent) {setupUI();}private:void setupUI() {QHBoxLayout* layout = new QHBoxLayout(this);layout->setContentsMargins(0, 0, 0, 0);QLabel* title = new QLabel(parent()->windowTitle());layout->addWidget(title);QPushButton* minBtn = new QPushButton("最小化");QPushButton* maxBtn = new QPushButton("最大化");QPushButton* closeBtn = new QPushButton("关闭");layout->addWidget(minBtn);layout->addWidget(maxBtn);layout->addWidget(closeBtn);connect(minBtn, &QPushButton::clicked, parent(), &QWidget::showMinimized);connect(maxBtn, &QPushButton::clicked, parent(), &QWidget::showMaximized);connect(closeBtn, &QPushButton::clicked, parent(), &QWidget::close);}
};// 主窗口类
class MainWindow : public QMainWindow {
public:MainWindow() {setWindowFlags(Qt::FramelessWindowHint);resize(800, 600);CustomTitleBar* titleBar = new CustomTitleBar(this);setMenuWidget(titleBar);}protected:bool dragging = false;QPoint dragPos;void mousePressEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = true;dragPos = event->globalPos() - pos();event->accept();}}void mouseMoveEvent(QMouseEvent* event) override {if (dragging) {move(event->globalPos() - dragPos);event->accept();}}void mouseReleaseEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = false;}}void resizeEvent(QResizeEvent* event) override {QMainWindow::resizeEvent(event);if (menuWidget()) {menuWidget()->setGeometry(QRect(0, 0, width(), menuWidget()->height()));}}
};// 另一个窗口类
class AnotherWindow : public QMainWindow {
public:AnotherWindow() {setWindowFlags(Qt::FramelessWindowHint);resize(400, 300);CustomTitleBar* titleBar = new CustomTitleBar(this);setMenuWidget(titleBar);}protected:bool dragging = false;QPoint dragPos;void mousePressEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = true;dragPos = event->globalPos() - pos();event->accept();}}void mouseMoveEvent(QMouseEvent* event) override {if (dragging) {move(event->globalPos() - dragPos);event->accept();}}void mouseReleaseEvent(QMouseEvent* event) override {if (event->button() == Qt::LeftButton) {dragging = false;}}void resizeEvent(QResizeEvent* event) override {QMainWindow::resizeEvent(event);if (menuWidget()) {menuWidget()->setGeometry(QRect(0, 0, width(), menuWidget()->height()));}}
};int main(int argc, char* argv[]) {QApplication app(argc, argv);MainWindow mainWindow;AnotherWindow anotherWindow;mainWindow.show();anotherWindow.show();return app.exec();
}

7. 功能说明

无边框窗口:通过 setWindowFlags(Qt::FramelessWindowHint) 实现。

  • 自定义标题栏:创建 CustomTitleBar 类,包含标题和按钮。
  • 窗口移动:重写鼠标事件 (mousePressEvent, mouseMoveEvent, mouseReleaseEvent) 实现拖动窗口。
  • 窗口调整大小:重写 resizeEvent,确保标题栏正确显示。
  • 复用标题栏:CustomTitleBar 作为独立组件,多个窗口可复用。
http://www.dtcms.com/a/288747.html

相关文章:

  • 分享如何在Window系统的云服务器上部署网站及域名解析+SSL
  • kotlin 扩展函数 在链式调用的妙用
  • LINUX入门(二)QT的安装及运行环境搭建
  • jmeter如何做自动化接口测试?
  • 元宇宙经济的四个特征
  • Spring Boot中REST与gRPC并存架构设计与性能优化实践指南
  • Dify 1.6 安装与踩坑记录(Docker 方式)
  • LeetCode 198 打家劫舍 LeetCode 213.打家劫舍II
  • 华为开源自研AI框架昇思MindSpore应用案例:基于ERNIE模型实现对话情绪识别
  • [Python] -项目实战4- 利用Python进行Excel批量处理
  • 基于pyside6的通用机器人遥控控制界面
  • client-go: k8s选主
  • JAVA面试宝典 -《容灾设计:异地多活架构实践》
  • go-redis Pipeline 与事务
  • 民法学学习笔记(个人向) Part.1
  • 如何应对“躺平”文化对项目的冲击
  • 生物化学笔记:安全防护 射频和微波辐射防护 电磁辐射与防护 生物电磁学
  • 《镜语者》
  • 技术演进中的开发沉思-40 MFC系列:多线程协作
  • AI-Compass 前沿速览:ChatGPT Agent、Kimi2、Mistral 语音模型、Grok AI 情感陪伴、百度 Tizzy、有言数字人
  • java学习6--方法
  • 深入解析定点数移位运算:原理、规则与实例
  • Golang的微服务链路追踪
  • github 近期热门项目-2025.7.20
  • RabbitMQ面试精讲 Day 4:Queue属性与消息特性
  • 【图论】图的定义与一些常用术语
  • RabbitMQ:解锁高效消息传递的密码[特殊字符]
  • UE为什么FlipFlop按快了会触发Bug?
  • 【愚公系列】《MIoT.VC》002-构建基本仿真工作站(布局一个基本工作站)
  • springboot注册servlet