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. Unable to get transparent title bar in macOS since Qt 6.4
Forum Updated to NodeBB v4.3 + New Features

Unable to get transparent title bar in macOS since Qt 6.4

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • X Offline
    X Offline
    xarli
    wrote on last edited by xarli
    #1

    Hi,

    Since Qt 6.4, the title bar on macOS is not transparent anymore
    01b00364-173a-4810-bb66-41d856d7dfb4-image.png

    This was the same code in Qt 6.3
    b7445884-8ad1-4e13-aedc-d2adda6e0fb6-image.png

    void hideWindowTitleBar(QMainWindow& window) {
        window.setUnifiedTitleAndToolBarOnMac(true);
    
        NSView* nativeView = reinterpret_cast<NSView*>(window.winId());
        NSWindow* nativeWindow = [nativeView window];
    
        [nativeWindow setStyleMask:[nativeWindow styleMask] | NSWindowStyleMaskFullSizeContentView | NSWindowTitleHidden];
        [nativeWindow setTitlebarAppearsTransparent:YES];
        [nativeWindow center];
    }
    

    This is the code that Im applying all the time, can somebody give me some clue?

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You might want to add the macOS version as well.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      X 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You might want to add the macOS version as well.

        X Offline
        X Offline
        xarli
        wrote on last edited by
        #3

        @SGaist Sure

        Can be replicated in both Big Sur and Ventura

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xarli
          wrote on last edited by xarli
          #4

          Anyone can bring some light here? please.

          SGaistS 1 Reply Last reply
          0
          • X xarli

            Anyone can bring some light here? please.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I tested your code on Monterey with 5.15.2, 6.3.2, 6.4.3 and 6.5.0 and with neither of them it worked.

            Can you provide a complete minimal example ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            X 2 Replies Last reply
            0
            • SGaistS SGaist

              I tested your code on Monterey with 5.15.2, 6.3.2, 6.4.3 and 6.5.0 and with neither of them it worked.

              Can you provide a complete minimal example ?

              X Offline
              X Offline
              xarli
              wrote on last edited by xarli
              #6

              @SGaist Sorry the delay to answer, I was out, I have a CMake project so is impossible to share here, I will provide an example done in Qt Creator during the week, thank for your time!

              1 Reply Last reply
              0
              • SGaistS SGaist

                I tested your code on Monterey with 5.15.2, 6.3.2, 6.4.3 and 6.5.0 and with neither of them it worked.

                Can you provide a complete minimal example ?

                X Offline
                X Offline
                xarli
                wrote on last edited by
                #7

                @SGaist Hi,

                I just uploaded an example using QMainWindow (based on mainwindow example) and QWebEngineView as CentralWidget https://we.tl/t-6Nr9CuOpxu in the order that my actual code use that.

                If I execute, the title bar is not transparent
                8b8be4c0-534d-4508-886b-336d3e9c226f-image.png

                But if I move hideWindowTitleBar(*this); after setCentralWidget
                aa9934f0-40cf-40b6-9530-a5d194cad9a3-image.png

                It works as expected again, Im trying to understand what Im doing in my code after setting setCentralWidget because the trick doesnt behave the same that in QtCreator even hideWindowTitleBar(*this); is the last thing I do before showing the window and modifying the content

                I will post here some findings, thanks for your time @SGaist.

                SGaistS 1 Reply Last reply
                0
                • X xarli

                  @SGaist Hi,

                  I just uploaded an example using QMainWindow (based on mainwindow example) and QWebEngineView as CentralWidget https://we.tl/t-6Nr9CuOpxu in the order that my actual code use that.

                  If I execute, the title bar is not transparent
                  8b8be4c0-534d-4508-886b-336d3e9c226f-image.png

                  But if I move hideWindowTitleBar(*this); after setCentralWidget
                  aa9934f0-40cf-40b6-9530-a5d194cad9a3-image.png

                  It works as expected again, Im trying to understand what Im doing in my code after setting setCentralWidget because the trick doesnt behave the same that in QtCreator even hideWindowTitleBar(*this); is the last thing I do before showing the window and modifying the content

                  I will post here some findings, thanks for your time @SGaist.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Reducing the code to something minimal, it is working setting the central widget either before or after the call to hideWindowTitleBar.
                  main.cpp

                  #include "mainwindow.h"
                  
                  #include <QApplication>
                  
                  int main(int argc, char **argv)
                  {
                      QApplication app(argc, argv);
                      MainWindow mainWin;
                      mainWin.resize(800, 600);
                      mainWin.show();
                      return app.exec();
                  }
                  

                  mainwindow.h

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = nullptr);
                  };
                  
                  #endif // MAINWINDOW_H
                  

                  mainwindow.cpp

                  #include "mainwindow.h"
                  #include "mainwindowmac.h"
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                  {
                      setWindowTitle("Qt Transparent");
                  
                      QWidget *centralWidget = new QWidget();
                      centralWidget->setStyleSheet("background: yellow");
                      hideWindowTitleBar(this);
                      setCentralWidget(centralWidget);
                  }
                  

                  mainwindowmac.h

                  #ifndef MAINWINDOWMAC_H
                  #define MAINWINDOWMAC_H
                  
                  class QMainWindow;
                  
                  void hideWindowTitleBar(QMainWindow *window);
                  
                  #endif // MAINWINDOWMAC_H
                  

                  mainwindowmac.mm

                  #include "mainwindowmac.h"
                  #include <Cocoa/Cocoa.h>
                  #include <QMainWindow>
                  
                  void hideWindowTitleBar(QMainWindow *window) {
                      window->setUnifiedTitleAndToolBarOnMac(true);
                  
                      NSView* nativeView = reinterpret_cast<NSView*>(window->winId());
                      NSWindow* nativeWindow = [nativeView window];
                  
                      [nativeWindow setStyleMask:[nativeWindow styleMask] | NSWindowStyleMaskFullSizeContentView | NSWindowTitleHidden];
                      [nativeWindow setTitlebarAppearsTransparent:YES];
                      [nativeWindow center];
                  }
                  

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  1
                  • Z Offline
                    Z Offline
                    zoy_l
                    wrote on last edited by zoy_l
                    #9
                    #import "mac_manager.h"
                    #import <Cocoa/Cocoa.h>
                    
                    // Declare a custom NSView subclass
                    @interface DraggableTitleView : NSView
                    @end
                    
                    @implementation DraggableTitleView
                    
                    // Handle mouse click events
                    - (void)mouseDown:(NSEvent *)event {
                      // double-click to zoom
                      if ([event clickCount] == 2) {
                        [self.window zoom:nil];
                      } else {
                        // drag Window
                        [self.window performWindowDragWithEvent:event];
                      }
                    }
                    
                    @end
                    
                    void MacManager::removeTitleBarFromWindow() {
                     // The position of the native buttons
                      CGPoint nativeButtonPoint = CGPointMake(100, 7);
                      
                      // The height of the custom title bar, Dragging the title bar will move the window
                      int titleBarViewHeight = 52;
                    
                      QWindowList windows = QGuiApplication::allWindows();
                    
                      auto item = windows.first();
                      auto winId = item->winId();
                    
                      auto *nativeView = reinterpret_cast<NSView *>(winId);
                      NSWindow *nativeWindow = [nativeView window];
                    
                      // Hide standard title bar
                      [nativeWindow setTitlebarAppearsTransparent:YES];
                      [nativeWindow setTitleVisibility:NSWindowTitleHidden];
                      [nativeWindow setStyleMask:[nativeWindow styleMask] | NSWindowStyleMaskFullSizeContentView];
                    
                      // Gets the dimensions of the window contents view
                      NSRect contentViewBounds = nativeWindow.contentView.bounds;
                    
                      // Create a custom Title Bar view
                      DraggableTitleView *titleBarView = [[DraggableTitleView alloc] initWithFrame:NSMakeRect(0, 0, contentViewBounds.size.width, titleBarViewHeight)];
                      titleBarView.autoresizingMask = NSViewWidthSizable;
                      titleBarView.wantsLayer = YES;
                      titleBarView.layer.backgroundColor = [[NSColor clearColor] CGColor];
                    
                      // Add a custom title bar view to the window's contents view
                      [nativeWindow.contentView addSubview:titleBarView];
                    
                      // Adjust the native button positions to fit in the new title bar
                      NSButton *closeButton = [nativeWindow standardWindowButton:NSWindowCloseButton];
                      NSButton *minimizeButton = [nativeWindow standardWindowButton:NSWindowMiniaturizeButton];
                      NSButton *zoomButton = [nativeWindow standardWindowButton:NSWindowZoomButton];
                    
                      closeButton.translatesAutoresizingMaskIntoConstraints = NO;
                      minimizeButton.translatesAutoresizingMaskIntoConstraints = NO;
                      zoomButton.translatesAutoresizingMaskIntoConstraints = NO;
                    
                      NSLayoutConstraint *closeButtonLeftContain = [NSLayoutConstraint constraintWithItem:closeButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:closeButton.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:nativeButtonPoint.x];
                      NSLayoutConstraint *closeButtonTopContain = [NSLayoutConstraint constraintWithItem:closeButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:closeButton.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:nativeButtonPoint.y];
                      closeButtonLeftContain.active = YES;
                      closeButtonTopContain.active = YES;
                      NSLayoutConstraint *minimizeButtonLeftContain = [NSLayoutConstraint constraintWithItem:minimizeButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:minimizeButton.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20 + nativeButtonPoint.x];
                      NSLayoutConstraint *minimizeButtonTopContain = [NSLayoutConstraint constraintWithItem:minimizeButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:minimizeButton.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:nativeButtonPoint.y];
                      minimizeButtonLeftContain.active = YES;
                      minimizeButtonTopContain.active = YES;
                      NSLayoutConstraint *zoomButtonLeftContain = [NSLayoutConstraint constraintWithItem:zoomButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:zoomButton.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:40 + nativeButtonPoint.x];
                      NSLayoutConstraint *zoomButtonTopContain = [NSLayoutConstraint constraintWithItem:zoomButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:zoomButton.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:nativeButtonPoint.y];
                      zoomButtonLeftContain.active = YES;
                      zoomButtonTopContain.active = YES;
                    }
                    

                    Help more people by implementing a fully customizable title bar here, supporting movable buttons, and retaining double-click to maximize and long-press drag events.

                    Have fun !

                    WX20240201-133216@2x.png

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Reducing the code to something minimal, it is working setting the central widget either before or after the call to hideWindowTitleBar.
                      main.cpp

                      #include "mainwindow.h"
                      
                      #include <QApplication>
                      
                      int main(int argc, char **argv)
                      {
                          QApplication app(argc, argv);
                          MainWindow mainWin;
                          mainWin.resize(800, 600);
                          mainWin.show();
                          return app.exec();
                      }
                      

                      mainwindow.h

                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H
                      
                      #include <QMainWindow>
                      
                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit MainWindow(QWidget *parent = nullptr);
                      };
                      
                      #endif // MAINWINDOW_H
                      

                      mainwindow.cpp

                      #include "mainwindow.h"
                      #include "mainwindowmac.h"
                      
                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                      {
                          setWindowTitle("Qt Transparent");
                      
                          QWidget *centralWidget = new QWidget();
                          centralWidget->setStyleSheet("background: yellow");
                          hideWindowTitleBar(this);
                          setCentralWidget(centralWidget);
                      }
                      

                      mainwindowmac.h

                      #ifndef MAINWINDOWMAC_H
                      #define MAINWINDOWMAC_H
                      
                      class QMainWindow;
                      
                      void hideWindowTitleBar(QMainWindow *window);
                      
                      #endif // MAINWINDOWMAC_H
                      

                      mainwindowmac.mm

                      #include "mainwindowmac.h"
                      #include <Cocoa/Cocoa.h>
                      #include <QMainWindow>
                      
                      void hideWindowTitleBar(QMainWindow *window) {
                          window->setUnifiedTitleAndToolBarOnMac(true);
                      
                          NSView* nativeView = reinterpret_cast<NSView*>(window->winId());
                          NSWindow* nativeWindow = [nativeView window];
                      
                          [nativeWindow setStyleMask:[nativeWindow styleMask] | NSWindowStyleMaskFullSizeContentView | NSWindowTitleHidden];
                          [nativeWindow setTitlebarAppearsTransparent:YES];
                          [nativeWindow center];
                      }
                      
                      J Offline
                      J Offline
                      JaxLi
                      wrote on last edited by
                      #10

                      @SGaist Hello, I copied you code and run with Qt6.8.2 on OSX 14.6.1 , it seems to have no effect. Has anything changed?
                      WX20250317-183725@2x.png

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JaxLi
                        wrote on last edited by
                        #11

                        After testing it with Qt6.5.3 I found that it does not work with Qt6.8.2.
                        https://bugreports.qt.io/browse/QTBUG-134797

                        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