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. Embed a QQuickView in a Cocoa View
Forum Updated to NodeBB v4.3 + New Features

Embed a QQuickView in a Cocoa View

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.7k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    vinxz
    wrote on last edited by
    #1

    Hi,
    Despite a lot of time spent on trying to solve that issue, I can't embed cleanly a QQuickView in a Cocoa View.
    the Cocoa view is created by a host, which gives me a NSView*, and I want to create a qml GUI, with a QQuickView.

    I explored 2 solution :

    solution 1 :
    parent_window = QWindow::fromWinId(WId(parent_view));
    quick_view = new QQuickView(parent_window);
    quick_view->show();

    in that case , the parent window remains empty, and a new window is created with the qml view

    solution 2 :
    NSView parentView = reinterpret_cast<NSView >(parent_view);
    NSView
    quickView = reinterpret_cast<NSView >(quick_view->winId());
    [parentView addSubview:quickView positioned:NSWindowAbove relativeTo:nil];

    in that case, the qml shows in the parent window, but an empty window is created anyway.

    Did somenone succed in having that done properly, which means displaying the qml in the parent window, without creating a useless window ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I haven't tested it with your use case however QMacNativeWidget might be of interest.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vinxz
        wrote on last edited by
        #3

        Hi,
        I already tried it...
        As it's buggy in c++, you have to create the object in obj-c.
        the result I got was the same as solution 2

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What do you mean by buggy in c++ ? You need to do some Objective-C++ in order to use that class.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vinxz
            wrote on last edited by
            #5

            Okay, anyway that's what I did !

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vinxz
              wrote on last edited by
              #6

              If I take the QMacNativeWidget example, and replace the pushbutton by a QQuickWidget, unfortunately the qml view is displayed in another window..

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Can you share the code that you are using ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  vinxz
                  wrote on last edited by
                  #8

                  Hi,
                  the last code I am using is the following :

                  in the .cpp file :

                  QQuickWidget* quickWidget = new QQuickWidget();
                  [...]
                  createMacView(inParent, quickWidget);
                  

                  in the .mm file :

                  void createMacView(void* inView, QQuickWidget* inQuickWidget)
                  {
                      QMacNativeWidget *nativeWidget = new QMacNativeWidget();
                      nativeWidget->move(0, 0);
                      nativeWidget->setPalette(QPalette(Qt::black));
                      nativeWidget->setAutoFillBackground(true);
                      QVBoxLayout* layout = new QVBoxLayout();
                          inQuickWidget->setAttribute(Qt::WA_LayoutUsesWidgetRect);
                      layout->addWidget(inQuickWidget);
                      nativeWidget->setLayout(layout);
                      
                    
                   // Adjust Cocoa layouts
                      NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId());
                      NSView *contentView = reinterpret_cast<NSView *>(inView);
                      
                      
                   [nativeWidgetView setFrame: NSMakeRect (0, 0, inQuickWidget->geometry().width(), inQuickWidget->geometry().height())];
                      
                     // Add the nativeWidget to the window.
                      [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil];
                      nativeWidget->show();
                      inQuickWidget->show();
                  }
                  

                  In that case, the qml is displayed on the host window, but a useless windows is created anyway (we can see its "close / minimize / maximize" bar at 0,0)

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vinxz
                    wrote on last edited by
                    #9
                        up !
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Can you also provide a main.cpp file ?

                      This will allow to re-create the same conditions you have.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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