Qt全屏显示时自定义任务栏

遇到一个奇葩需求,既要我隐藏任务栏和标题栏做全屏,又要我有个标题栏显示信息,那就只能自定义标题栏了。由于这次程序最顶部是一个工具栏,自定义标题栏最大的问题是如何把标题栏加在工具栏的上边,核心问题是如何把工具栏向下移一个标题栏的高度,显示效果如下:

实现代码如下,省却了头文件定义,自己加一下就行

cpp 复制代码
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->showFullScreen();
    this->initGlobalTitleBar();
}

void MainWindow::initGlobalTitleBar()
{
    // 1. 创建全局标题栏(主窗口直接子控件,层级最高)
    QWidget *globalTitleBar = new QWidget(this);
    globalTitleBar->setObjectName("GlobalTitleBar");
    globalTitleBar->setFixedHeight(32);
    globalTitleBar->setVisible(true);
    // 强制置顶(关键:避免被其他控件覆盖)
    globalTitleBar->raise();
    // 样式:确保能看到,不透明
    globalTitleBar->setStyleSheet(R"(
        QWidget#GlobalTitleBar {
            background-color: #2c3e50;
            border-bottom: 2px solid #00BB9E;
        }
    )");

    // 2. 标题栏内容(软件名+关闭按钮)
    QLabel *logoLabel = new QLabel(globalTitleBar);
    logoLabel->setFixedSize(30, 30);
    logoLabel->setScaledContents(false); // 关闭强制拉伸
    logoLabel->setAlignment(Qt::AlignCenter); // 图片居中显示
    logoLabel->setPixmap(QPixmap(":/icons/edit.png").scaled(28, 28, Qt::KeepAspectRatio, Qt::SmoothTransformation));

    QLabel *appNameLabel = new QLabel("你的软件名 v1.0", globalTitleBar);
    appNameLabel->setStyleSheet("color: white; font-size:14px; padding-left:15px;");
    QPushButton *closeBtn = new QPushButton(globalTitleBar);
    closeBtn->setIcon(QIcon(":/icons/exit.png"));
    closeBtn->setIconSize(QSize(32, 32));
    connect(closeBtn, &QPushButton::clicked, this, [=](){
        this->close();
    });

    // 3. 标题栏布局
    QHBoxLayout *titleLayout = new QHBoxLayout(globalTitleBar);
    titleLayout->setContentsMargins(5, 0, 5, 0); // 左右加5px边距,避免贴边
    titleLayout->setSpacing(0);
    titleLayout->addWidget(logoLabel);
    titleLayout->addWidget(appNameLabel);
    titleLayout->addStretch();
    titleLayout->addWidget(m_closeBtn);

    // 4. 强制设置标题栏位置(最顶部)
    globalTitleBar->setGeometry(0, 0, this->width(), 32);
    QToolBar *toolBar = ui->toolBar;
    if (toolBar) {
        // 关键:给QMainWindow的工具栏区域加顶部边距,让工具栏自动下移
        this->setContentsMargins(0, 32, 0, 0); // 顶部边距=标题栏高度(32px)
        // 确保工具栏可见(恢复被隐藏的可能)
        toolBar->setVisible(true);
        toolBar->setEnabled(true);
        qDebug() << "工具栏已恢复,可见性:" << toolBar->isVisible();
    }
}

// ========== 窗口显示后,确保标题栏置顶 ==========
void MainWindow::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
    QWidget *titleBar = this->findChild<QWidget*>("GlobalTitleBar");
    if (titleBar) titleBar->raise(); // 防止标题栏被工具栏覆盖
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QMainWindow::resizeEvent(event);
    // 仅更新标题栏宽度,工具栏由Qt自动管理,无需手动调
    QWidget *titleBar = this->findChild<QWidget*>("GlobalTitleBar");
    if (titleBar) {
        titleBar->setGeometry(0, 0, this->width(), 32);
        titleBar->raise(); // 始终置顶
    }
}
相关推荐
Legendary_0081 小时前
LDR6500:USB‑C DRP PD协议芯片技术详解与应用实践
c语言·开发语言
2301_800976931 小时前
正则表达式
开发语言·python·正则表达式
故事还在继续吗1 小时前
C++20关键特性
开发语言·c++·c++20
青少儿编程课堂2 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
AIFarmer3 小时前
【无标题】
开发语言·c++·算法
昇腾CANN3 小时前
TileLang-Ascend 算子性能优化方法与实操
开发语言·javascript·性能优化·昇腾·cann
沐知全栈开发3 小时前
ionic 手势事件详解
开发语言
lsx2024063 小时前
Bootstrap 按钮
开发语言
神仙别闹3 小时前
基于 Python 实现 BERT 的情感分析模型
开发语言·python·bert