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. [SOLVED]integrate QML into existing C++
Qt 6.11 is out! See what's new in the release blog

[SOLVED]integrate QML into existing C++

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.4k Views 1 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.
  • J Offline
    J Offline
    jfabiani
    wrote on last edited by
    #1

    Every tutorial (and all my google searchs) on using QML - demo's using QML from the start. I have an existing QT project with lot's of UI files (actually not my code). I want to be able to code a QML dialog and and then from a c++ menu display it. I'm trying to learn - how to use QML. Below is my code that I added to main.cpp - but it opens a second window (not part of my mainwindow) and does not display correctly - I get a blank white window.

    Can someone please explain how to integrate QML into an exist C++ with lots of forms already?

    void showTest()
    {
    QQmlEngine engine;
    QQmlContext *context = new QQmlContext(engine.rootContext());

    QQuickView view(QUrl::fromLocalFile("guiclient/test.qml"));
    QQmlComponent component(&engine, QUrl::fromLocalFile("/u/Download/postbooks_source/qt-client/guiclient/test.qml"));
    

    /*if (component.status() == QQmlComponent::Ready) {
    QObject object = component.create(context);
    //object->show();
    }
    else
    qDebug(component.errorString().toLatin1());
    /

    view.show();
    // removing below prevents the window from opening.
    QEventLoop loop;
    loop.exec();
    return;
    }

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jfabiani
      wrote on last edited by
      #2

      It looks like this is not going to work at least not in 5.1.
      http://qt-project.org/forums/viewthread/23190

      Thanks for those who viewed

      Johnf

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        That functionality was added in Qt 5.1: http://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jfabiani
          wrote on last edited by
          #4

          I have attempted to use the example but I must be missing something!

          I'm guessing that the "widgetLayout->" is the parent?? Since other things did not work.

          Could you provide a sample of a function that really works. BTW others are suggesting that it can't be done until 5.2. I know I'm asking a lot but I was about to give up on Qt until I saw your post. I'm still not to sure it works? No I'm not saying it's BS I just don't understand it.

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            Where did you see that suggestion? It can definitely be done in Qt 5.1 -- I'm doing it in a project of mine.

            widgetLayout is a QLayout in the parent QWidget. You cannot add a child widget directly into the parent widget; you need to add it to the parent's layout. See the "Layout Management":http://qt-project.org/doc/qt-5.1/qtwidgets/layout.html page for details.

            Here's how I did it:
            @
            MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
            {
            // Integrate the UI file into C++
            setupUi(this);

            // Create the QML view
            QQuickView* quickView = new QQuickView(QUrl("MyScene.qml"));
            
            // Make the QML view resize when the parent is resized
            quickView->setResizeMode(QQuickView::SizeRootObjectToView);
            
            // Add the QML view to my widget layout (centralVBoxLayout),
            // which I added in Qt Designer
            QWidget* quickWidget = QWidget::createWindowContainer(quickView);
            this->centralVBoxLayout->addWidget(quickWidget);
            

            }
            @

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jfabiani
              wrote on last edited by
              #6

              That seems to work - thanks.

              BTW - I got the suggestion from two (2) people (one on Qt private IRC) and the other from a friend trying to do something similar.

              As a noob I'm learning at a very high curve (I haven't programmed in C++ in 20 years) and the tutorials do not cover what I'm trying to do (that is integrate QML into an exist C++ Qt program).

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                Please don't be discouraged; we all start as newbies but we all improve with practice :)

                There are many tutorials/examples about integrating QML into a C++ program. I think one of the issues is that there's too many tutorials, so it's hard for a newcomer to know what to read! This is probably a good place to start for you: https://qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html

                If you have any more questions about a specific task you want to do, feel free to make a new post and someone will point you in the right direction.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                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