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 backing:NSBackingStoreBuffered defer:NO]; // NSView NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(200, 200, 200, 200)]; // Widgets QMacNativeWidget *nativeWidget = new QMacNativeWidget(); nativeWidget->setPalette(QPalette(Qt::red)); nativeWidget->setAutoFillBackground(true); QVBoxLayout *layout = new QVBoxLayout(); QPushButton *pushButton = new QPushButton("Button", nativeWidget); layout->addWidget(pushButton); nativeWidget->setLayout(layout); // Add Native Widget To NSView Before Adding NSView To NSWindow (This Doesn't Work) [view addSubview:nativeWidget->nativeView()]; nativeWidget->show(); // 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];
Thanks
-
Perhaps one solution is to call exposeWindow() on viewDidMoveToWindow() every time by removing the "if (!_q_NSWindowDidChangeOcclusionStateNotification && m_isMenuView)" check? (Note: This is in qsnview.mm)
- (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; m_platformWindow->exposeWindow(); } } else if (m_exposedOnMoveToWindow) { m_exposedOnMoveToWindow = false; m_platformWindow->obscureWindow(); } }