Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding QMacNativeWidget to a NSView without an NSWindow
QtWS25 Last Chance

Adding QMacNativeWidget to a NSView without an NSWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    inhwan
    wrote on last edited by inhwan
    #1

    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

    1 Reply Last reply
    0
    • I Offline
      I Offline
      inhwan
      wrote on last edited by inhwan
      #2

      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();
          }
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved