当前位置: 首页 > 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.xdnf.cn/news/1154827.html

相关文章:

  • 2025 年科技革命时刻表:四大关键节点将如何重塑未来?
  • 详解Mysql Order by排序底层原理
  • RK3588 编译 Android 13 镜像方法
  • 用C语言实现控制台应用的按键方向控制
  • Qt的安装和环境配置
  • 【愚公系列】《MIoT.VC》002-构建基本仿真工作站(布局一个基本工作站)
  • OPC UA, CAN, PROFINET, SOCKET, MODBUS, HTTP, S7七种物联网常用协议解释
  • 金融工程、金融与经济学知识点
  • Claude 3模型深度剖析:架构创新与性能突破
  • JAVA面试宝典 -《容灾设计:异地多活架构实践》
  • 从零搭建智能搜索代理:LangGraph + 实时搜索 + PDF导出完整项目实战
  • 从TPACK到TPACK - AI:人工智能时代教师知识框架的重构与验证
  • Kubernetes中为ELK组件配置持久化存储
  • nginx定期清理日志
  • 线程池的状态
  • AI开发 | 基于FastAPI+React的流式对话
  • sqli-labs通关笔记-第09关 GET时间盲注(单引号闭合 手工注入+脚本注入两种方法)
  • Docker Desktop 入门教程(Windows macOS)
  • Elasticsearch 简化指南:GCP Google Compute Engine
  • 相似度计算
  • COGNEX康耐视IS5403-01智能相机加Navitar 18R00 LR1010WM52镜头
  • IP协议介绍
  • GPT-4o mini TTS:领先的文本转语音技术
  • VTM 是“H.266/VVC 标准的官方参考软件”视频分析,入门教程,它存在的唯一目的就是“让学术界和工业界在同一把尺子上做实验
  • Docker 在 Ubuntu 系统中的详细操作指南
  • 事务的传播行为,分别在spring和mysql中讲解
  • CentOS 服务器docker pull 拉取失败
  • 相机模型和对极几何
  • MySQL(147)如何进行跨平台迁移?
  • 【LeetCode 热题 100】124. 二叉树中的最大路径和——DFS