Embed a QQuickView in a Cocoa View

  • 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);

    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);
    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 ?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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

    Hope it helps

  • 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

  • Lifetime Qt Champion

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

  • Okay, anyway that's what I did !

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

  • Lifetime Qt Champion

    Can you share the code that you are using ?

  • 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);
        QVBoxLayout* layout = new QVBoxLayout();
     // 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];

    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)

  •     up !

  • Lifetime Qt Champion

    Can you also provide a main.cpp file ?

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.