Hello everyone,

I'm using the latest Qt 5.7 and I am having a bit of a problem with what would seem to be a relatively straight-forward task. I'm trying to embed a QML based view hierarchy onto a native application on OSX. Here's what I've tried so far:

I started simple. Just to launch a QQuickView, like so:

Qt Code:
  1. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  2. {
  3. // Insert code here to initialize your application
  4. NSWindow* window = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
  5. NSView *view = [window contentView];
  6.  
  7. char* test[0];
  8. int count = 0;
  9.  
  10. QApplication::instance()->setAttribute(Qt::AA_PluginApplication);
  11. _qt = new QApplication(count, test);
  12.  
  13. _qml = new QQuickView();
  14. _qml->setSource(QUrl(QStringLiteral("main.qml")));
  15. _qml->setResizeMode(QQuickView::SizeRootObjectToView);
  16. _qml->show();
  17.  
  18. _timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(processEvents) userInfo:nil repeats:YES];
  19. }
To copy to clipboard, switch view to plain text mode 

The qml is just a simple thing:

Qt Code:
  1. import QtQuick 2.7
  2.  
  3. Item
  4. {
  5. visible: true
  6. x: 0;
  7. y: 0;
  8. width: 100
  9. height: 100
  10. Rectangle
  11. {
  12. id: rect
  13. anchors.fill: parent
  14. color: 'blue'
  15. MouseArea
  16. {
  17. anchors.fill: parent
  18. onClicked:
  19. {
  20. console.log(parent.color);
  21. if(parent.color == '#0000ff')
  22. parent.color = 'green';
  23. else
  24. parent.color = 'blue';
  25. }
  26. }
  27. }
  28. }
To copy to clipboard, switch view to plain text mode 

The timer is used to call processEvents and sendPostedEvents on the qt app instance. This works, however, it is launching its own window. What I'd like to do is embed this into the native window of the application. And this is where things don't work. I tried this:

Qt Code:
  1. NSView *view = [window contentView];
  2. _qml = new QQuickView();
  3. _qml->setSource(QUrl(QStringLiteral("main.qml")));
  4. _qml->setResizeMode(QQuickView::SizeRootObjectToView);
  5.  
  6. [view addSubview:(NSView*)_qml->winId()];
To copy to clipboard, switch view to plain text mode 

This results in the QML indeed being inserted into the main view, but the rectangle is black and although it receives the clicks ( I see the logs) it doesn't render the rectangle properly (its supposed to change its color)

I also tried this, but it is not working either (launches a borderless window)

Qt Code:
  1. _qWindow = QWindow::fromWinId((WId)view);
  2.  
  3. _qml = new QQuickView(_qWindow);
  4. _qml->setSource(QUrl(QStringLiteral("main.qml")));
  5. _qml->setResizeMode(QQuickView::SizeRootObjectToView);
  6. _qml->show();
  7. _qWindow->show();
To copy to clipboard, switch view to plain text mode 

Can someone clue me in on how to achieve this? It 'should' be something easy that I'm missing. Could you provide an example ?

Thanks!