Unsolved 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?
Thanks!
-
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 () -(void)processEvents; @property(nonatomic) NSTimer* timer; @property(nonatomic) QApplication* qt; @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application NSWindow* window = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; NSView *view = [window contentView]; assert(view); char* test[0]; int count = 0; QApplication::instance()->setAttribute(Qt::AA_MacPluginApplication); _qt = new QApplication(count, test); QMacNativeWidget* native = new QMacNativeWidget(view); assert(native); QQuickWidget* qml = new QQuickWidget(native); qml->setSource(QUrl(QStringLiteral("main.qml"))); QVBoxLayout* layout = new QVBoxLayout(); layout->addWidget(qml); native->setLayout(layout); qml->show(); native->show(); 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]; _qt->quit(); } -(void)processEvents { _qt->processEvents(); _qt->sendPostedEvents(0,-1); } @end
and here's the QML:
import QtQuick 2.7 Item { visible: true x: 0; y: 0; width: 100 height: 100 Rectangle { anchors.fill: parent color: 'blue' MouseArea { anchors.fill: parent onClicked: { console.log(parent.color); if(parent.color == '#0000ff') parent.color = 'green'; else parent.color = 'blue'; } } } }