Important: Please read the Qt Code of Conduct -

Adding QMacNativeWidget to a NSView without an NSWindow

  • Hi,

    As per the title I was wondering if there are any restrictions in adding a QMacNativeWidget to a NSView without an NSWindow (i.e. The NSView 'window' property is nil).

    I am adding my NSView to the NSWindow after adding the QMacNativeWidget which leads to the QMacNativeWidget not rendering. If I add my NSView to the NSWindow before adding the QMacNativeWidget it works fine.

    The trouble is I need to add my NSView to the NSWindow after adding the QMacNativeWidget. I am wondering if the lack of ability to do this is a Qt bug or if I am simply using it wrong.

    Here is a (minimal) code snippet to illustrate the issue:

        qtApp = new QApplication(qtArgc, qtArgv);
        // NSWindow
        NSWindow* window  = [[NSWindow alloc] initWithContentRect:NSMakeRect(500, 500, 500, 500)
          styleMask:NSTitledWindowMask |  NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
        // NSView
        NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(200, 200, 200, 200)];
        // Widgets
        QMacNativeWidget *nativeWidget = new QMacNativeWidget();
        QVBoxLayout *layout = new QVBoxLayout();
        QPushButton *pushButton = new QPushButton("Button", nativeWidget);
        // Add Native Widget To NSView Before Adding NSView To NSWindow (This Doesn't Work)
        [view addSubview:nativeWidget->nativeView()];
        // Add NSView To NSWindow
        [view setWantsLayer:YES];
        [window.contentView addSubview:view];
        // Add Native Widget To NSView After Adding NSView To NSWindow (This Works)
        // [view addSubview:nativeWidget->nativeView()];
        // nativeWidget->show();
        [window makeKeyAndOrderFront:window];


  • Perhaps one solution is to call exposeWindow() on viewDidMoveToWindow() every time by removing the "if (!_q_NSWindowDidChangeOcclusionStateNotification && m_isMenuView)" check? (Note: This is in

    - (void)viewDidMoveToWindow
        m_backingStore = Q_NULLPTR;
        m_isMenuView = [self.window.className isEqualToString:@"NSCarbonMenuWindow"];
        if (self.window) {
            // This is the case of QWidgetAction's generated QWidget inserted in an NSMenu.
            // 10.9 and newer get the NSWindowDidChangeOcclusionStateNotification
            if (!_q_NSWindowDidChangeOcclusionStateNotification && m_isMenuView) { // Remove This?
                m_exposedOnMoveToWindow = true;
        } else if (m_exposedOnMoveToWindow) {
            m_exposedOnMoveToWindow = false;

Log in to reply