Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Qt6.8.3] Why does QToolBar perform inconsistently on qt5 and qt6?
Forum Updated to NodeBB v4.3 + New Features

[Qt6.8.3] Why does QToolBar perform inconsistently on qt5 and qt6?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 79 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    victorman
    wrote last edited by
    #1

    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.
    b655cb4a-9bd1-4f15-9347-ce7bbe8779a3-image.png

    This is the effect when using Qt6.8.3
    1f80bf1c-8dbb-4425-bae1-30d6477befba-image.png

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      victorman
      wrote last edited by
      #2

      I found that in QT version 6.8.3, when using the styleMask of NSWindow, only MainWindow itself would extend to the title bar, while CentralWidget and ToolBar would not extend to the title bar.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved