4

I have the following code in a Qt project, and I want to set the titlebarAppearsTransparent variable for a window to true in Objective-C. The program compiles correctly, but it crashes when it reaches [&w titlebarAppearsTransparent:YES]; Is what I'm trying to do even possible, and if so how do I fix it?

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include "globals.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <AppKit/NSWindow.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QApplication::setOrganizationName("Siddha Tiwari");
    QApplication::setApplicationName("NarwhalEdit");

    MainWindow *w = new MainWindow();

    [&w titlebarAppearsTransparent:YES];

    setTheme(true);

    w->show();

    return a.exec();
}
Siddha Tiwari
  • 125
  • 3
  • 7
  • Can't test right now, but did you have a look at the bugtracker [here](https://bugreports.qt.io/browse/QTBUG-63444) ? In particular, Look at the code in the comments. – Sergio Monteleone Apr 18 '18 at 08:24
  • Worth noting you can use Qt [QtMacExtras](https://doc.qt.io/qt-5.10/qtmacextras-index.html) classes with Qt, while thats not really mixing code, you still can mix code following these posts [Mixing Qt C++ with Objective-C](https://forum.qt.io/topic/39527/mixing-qt-c-with-objective-c-without-quick) .. and [Calling Objective-C method from C++ method?](https://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method) – Mohammad Kanan Apr 18 '18 at 14:05
  • @S.Monteleone Thanks, that solution in the bugtracker worked – Siddha Tiwari Apr 19 '18 at 01:07
  • @SiddhaTiwari: glad it worked. I wrote down a full answer, for convenience. – Sergio Monteleone Apr 19 '18 at 07:36

1 Answers1

4

It is possible to accomplish this using the native API, as reported here, obtaining the NSWindow pointer from QWidget::window()::winId().

I would also suggest to wrap the code with conditional compiling directives, so it is ignored when compiling for other platforms.

Here is a snippet (assuming w is the pointer to your QMainWindow):

#ifdef Q_OS_MAC

QCoreApplication::setAttribute( Qt::AA_DontCreateNativeWidgetSiblings );
NSView *nsview = ( __bridge NSView * )reinterpret_cast<void *>( w->window()->winId() );
NSWindow *nswindow = [nsview window];
nswindow.titlebarAppearsTransparent = YES;

#endif
Sergio Monteleone
  • 2,776
  • 9
  • 21