[Qt6.8.3] Why does QToolBar perform inconsistently on qt5 and qt6?
-
I am currently working on migrating my application from Qt5 to Qt6.
However, I found that the method I used to customize the MacOS title bar in Qt5 is no longer effective in Qt6.
I wrote a minimal demo to reproduce this problem.
This is the effect when using Qt5.15.12.

This is the effect when using Qt6.8.3

This is the mainwindow.mm.
#include "mainwindow.h" #include <QPushButton> #include <QToolBar> #include <QHBoxLayout> #include <QTimer> #include <QMenuBar> #include <QLabel> #include <QApplication> #include <AppKit/NSWindow.h> #include <Cocoa/Cocoa.h> #include <qdebug.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setStyleSheet("MainWindow { background-color: red; }"); setFixedSize(640, 640); QToolBar* toolBar = new QToolBar(this); toolBar->setFixedWidth(640); toolBar->setFixedHeight(30); toolBar->setStyleSheet("QToolBar {background-color: green;}"); QWidget* titleBarWidget = new QWidget(this); titleBarWidget->setFixedWidth(640); titleBarWidget->setFixedHeight(30); titleBarWidget->setStyleSheet("QWidget {background-color: blue;}"); QHBoxLayout* titleLayout = new QHBoxLayout(titleBarWidget); titleLayout->setContentsMargins(70, 0, 10, 0); QLabel* titleLabel = new QLabel("Custom Title Bar", titleBarWidget); titleLabel->setStyleSheet("QLabel { color: white; }"); QPushButton* btn1 = new QPushButton("Click Me", titleBarWidget); btn1->setStyleSheet("QPushButton { background-color: white}"); connect(btn1, &QPushButton::clicked, this, [this](bool){qDebug() << QString("Button clicked");}); titleLayout->addWidget(titleLabel); titleLayout->addStretch(); titleLayout->addWidget(btn1); titleBarWidget->setLayout(titleLayout); toolBar->layout()->addWidget(titleBarWidget); addToolBar(toolBar); } MainWindow::~MainWindow() {} void MainWindow::showEvent(QShowEvent *event) { QMainWindow::showEvent(event); qDebug() << "Qt Version:" << QT_VERSION_MAJOR << "." << QT_VERSION_MINOR << "." << QT_VERSION_PATCH; #ifdef Q_OS_MACOS QTimer::singleShot(0, this, [this]() { NSView* view = (__bridge NSView*)this->winId(); NSWindow* nsWindow = view.window; if (nsWindow) { nsWindow.titleVisibility = NSWindowTitleHidden; nsWindow.titlebarAppearsTransparent = YES; nsWindow.styleMask |= NSWindowStyleMaskFullSizeContentView; } }); #endif }Or do I have to implement these three buttons by myself?