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. How to display QML into a native window
Forum Updated to NodeBB v4.3 + New Features

How to display QML into a native window

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.9k Views 2 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.
  • L440L Offline
    L440L Offline
    L440
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • veryqtpersonV Offline
      veryqtpersonV Offline
      veryqtperson
      wrote on last edited by
      #2

      Perhaps, that's what you need?

      1 Reply Last reply
      0
      • L440L Offline
        L440L Offline
        L440
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • L440L Offline
            L440L Offline
            L440
            wrote on last edited by
            #5

            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';
                        }
                    }
                }
            }
            
            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