Skip to content

Instantly share code, notes, and snippets.

@zethon
Last active June 27, 2021 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zethon/dc7a5c71b0ac3cff35017febd1f15511 to your computer and use it in GitHub Desktop.
Save zethon/dc7a5c71b0ac3cff35017febd1f15511 to your computer and use it in GitHub Desktop.
Changing the color of macOS app title bar in Qt with CMake

In CMake, add/link the Carbon library to the executable

    FIND_LIBRARY(CARBON_LIBRARY Carbon)
    set(EXTRA_LIBS
        ${CARBON_LIBRARY}

Make sure to add ${CARBON_LIBRARY} in the TARGET_LINK_LIBRARIES call for the executable.

Then create a file to hold the Object-C code, in this case changetitlebarcolor.mm

#include <QWidget>
#import <Cocoa/Cocoa.h>

extern "C" void changeTitleBarColor(WId winId, double red, double green, double blue) 
{
    if (winId == 0) return;
    
    NSView* view = (NSView*)winId;
    NSWindow* window = [view window];
    window.titlebarAppearsTransparent = YES;
    window.backgroundColor = [NSColor colorWithRed:red green:green blue:blue alpha:0.];
}

Make sure to add the file to your source code files in your CMake project. Then define the function in your code like so:

extern "C" void changeTitleBarColor(WId winId, double red, double green, double blue);

And then call the function, ideally in your MainWindow constructor like so:

    changeTitleBarColor(winId(), 245./255., 29./255., 46./255.);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment