Qt 5.11 | Qt Widgets | C++ Classes | QMacNativeWidget

Qt 5.11.3 Reference Documentation

# QMacNativeWidget Class

The QMacNativeWidget class provides a widget for macOS that provides a way to put Qt widgets into Cocoa hierarchies. More...

Header: | #include <QMacNativeWidget>
qmake: | QT += widgets
Since: | Qt 4.5
Inherits: | QWidget

- List of all members, including inherited members

## Public Functions

| QMacNativeWidget(NSView *parentView = nullptr)
virtual | ~QMacNativeWidget()
NSView * | nativeView() const

## Reimplemented Public Functions

virtual QSize | sizeHint() const override

- 214 public functions inherited from QWidget

## Reimplemented Protected Functions

virtual bool | event(QEvent *ev) override

- 35 protected functions inherited from QWidget

### Additional Inherited Members

- 59 properties inherited from QWidget
- 19 public slots inherited from QWidget
- 3 signals inherited from QWidget
- 5 static public members inherited from QWidget
- 35 protected functions inherited from QWidget
- 1 protected slot inherited from QWidget

## Detailed Description

The QMacNativeWidget class provides a widget for macOS that provides a way to put Qt widgets into Cocoa hierarchies.

On macOS, there is a difference between a window and view; normally expressed as widgets in Qt. Qt makes assumptions about its parent-child hierarchy that make it complex to put an arbitrary Qt widget into a hierarchy of "normal" views from Apple frameworks. QMacNativeWidget bridges the gap between views and windows and makes it possible to put a hierarchy of Qt widgets into a non-Qt window or view.

QMacNativeWidget pretends it is a window (i.e. isWindow() will return true), but it cannot be shown on its own. It needs to be put into a window when it is created or later through a native call.

Here is an example showing how to put a QPushButton into a NSWindow:

          NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
          NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, app.desktop()->height() - 200, 239, 200)
                              styleMask:NSTitledWindowMask | NSClosableWindowMask
                                        | NSMiniaturizableWindowMask | NSResizableWindowMask
                              backing:NSBackingStoreBuffered defer:NO];

          QMacNativeWidget *nativeWidget = new QMacNativeWidget();
          nativeWidget->move(0, 0);
          nativeWidget->setPalette(QPalette(Qt::red));
          nativeWidget->setAutoFillBackground(true);
          QVBoxLayout *layout = new QVBoxLayout();
          QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget);
          pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle.
          layout->addWidget(pushButton);
          nativeWidget->setLayout(layout);

          // Adjust Cocoa layouts
          NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId());
          NSView *contentView = [window contentView];
          [contentView setAutoresizesSubviews:YES];
          [nativeWidgetView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
          [nativeWidgetView setAutoresizesSubviews:YES];
          NSView *pushButtonView = reinterpret_cast<NSView *>(pushButton->winId());
          [pushButtonView setAutoresizingMask:NSViewWidthSizable];

          // Add the nativeWidget to the window.
          [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil];
          nativeWidget->show();
          pushButton->show();

          // Show the window.
          [window makeKeyAndOrderFront:window];
          [pool release];

Note that QMacNativeWidget requires knowledge of Cocoa. All it does is get the Qt hierarchy into a window not owned by Qt. It is then up to the programmer to ensure it is placed correctly in the window and responds correctly to events.

## Member Function Documentation

### QMacNativeWidget::QMacNativeWidget(NSView *parentView = nullptr)

Create a QMacNativeWidget with parentView as its "superview" (i.e., parent). The parentView is a NSView pointer.

### `[virtual]` QMacNativeWidget::~QMacNativeWidget()

Destroy the QMacNativeWidget.

### `[override virtual protected]` bool QMacNativeWidget::event(QEvent *ev)

Reimplemented from QWidget::event().

### NSView *QMacNativeWidget::nativeView() const

Returns the native view backing the QMacNativeWidget.

### `[override virtual]` QSize QMacNativeWidget::sizeHint() const

Reimplemented from QWidget::sizeHint().