How to display QML into a native window

  • Hi,
    Could someone point me to an example on how to load a QML file within a native OS window, maybe using QMacNativeWidget?

    However, I do not want to spawn a new window, but rather, given a native window (or view?) handle to load a qml scene into it. Is there some reference code that could show me how to do that?


  • Perhaps, that's what you need?

  • Well, I tried that, but I have two problems with it. One is that it suffers from some mysterious crash if I create the QQuickWidget anywhere else but the stack where the QApplication is instantiated and the other is that it too launches its own window.

    What I really need is a way to embed this or some other Qt view into a given native window/view handle. Anything I tried so far, including QMacNativeWidget will result in an 'extra' window. Hence why I ask for some minimal trivial example code.

    Thanks for the suggestion though

  • @L440 said:

    is that it too launches its own window

    I doubt that. Only if you don't give it a parent (applies to all widgets). Can we see some code please?

  • Yes, sure. Thanks for looking into it. This is how I'm trying to do it. The code below is executed directly in the applicationDidFinishLaunching. Please ignore all the memory leaks.

    What I get is a result is two windows - the main one where the QML is indeed loaded and a 'ghost' window which is transparent but still has a frame in the center.

    I also don't see the qml view refreshing, it is supposed to change the rectangle's color when you click on it.

    @interface AppDelegate ()
    @property(nonatomic) NSTimer* timer;
    @property(nonatomic) QApplication* qt;
    @implementation AppDelegate
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
        // Insert code here to initialize your application
        NSWindow* window = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
        NSView *view = [window contentView];
        char* test[0];
        int count = 0;
        _qt = new QApplication(count, test);
        QMacNativeWidget* native = new QMacNativeWidget(view);
        QQuickWidget* qml = new QQuickWidget(native);
        QVBoxLayout* layout = new QVBoxLayout();
        NSView* qmlView = (NSView*)native->winId();
        [view addSubview:qmlView];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(processEvents) userInfo:nil repeats:YES];
    - (void)applicationWillTerminate:(NSNotification *)aNotification
        // Insert code here to tear down your application
        [_timer invalidate];

    and here's the QML:

    import QtQuick 2.7
        visible: true
        x: 0;
        y: 0;
        width: 100
        height: 100
            anchors.fill: parent
            color: 'blue'
                anchors.fill: parent
                    if(parent.color == '#0000ff')
                        parent.color = 'green';
                        parent.color = 'blue';

Log in to reply

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