Important: Please read the Qt Code of Conduct -

Calling qml on mainwindow

  • Hello. i try to call qml file with mainwindow class. here my code


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    class MainWindow : public QMainWindow {
        explicit MainWindow(QWidget *parent = 0);
    public slots:
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "qqmlapplicationengine.h"
    MainWindow::MainWindow(QWidget *parent) :
        QQmlApplicationEngine engine;


    #include <mainwindow.h>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        MainWindow w;;
        return app.exec();

    I can loading window normal but the window is not from qml it look like and empty window.

  • Moderators

    IMO, if you are trying to embed a QML file then a better option would be to use QQuickWidget

  • Hello, i'm thank for your response.

    After i use QQuickWidgets, When i run the project it seen to launch 2 window at the same time.

    I was create a qml project. and i see in my resource file has 2 qml file which name
    main.qml, mainform.ui.qml.

    1. if i use qqmlapplicaitonengine i will see the form suddenly close when it run
    2. if i use qquickview the window will launch normally but.. the message say
    QML debugging is enabled. Only use this in a safe environment.
    QQuickView does not support using windows as a root item. 
    If you wish to create your root window from QML, consider using QQmlApplicationEngine instead. 
    1. if i use qquickwidget it will see the 2 window main.qml, mainwindow.ui.qml will launch at the same time.

  • Lifetime Qt Champion

    Well, if you have a MainWindow AND a QML window then you get two windows. Do you really need this MainWindow? If not remove it and only use main.qml via QQmlApplicationEngine instead.

  • I'm sorry @jsulm i'm not i'm not good at c++ but i'm understand what your mean. Because i sperate a class name mainwindow and call it to main.cpp and i call the qml file inslude a class on file mainwindow.cpp. So the main problem is if i use qqmlapplicationengine it will suddenly close my window when it start ... I mean it will open and close at the same time..

  • Lifetime Qt Champion

    Here you can see an example:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine("main.qml");
        return app.exec();

    Are you doing it now this way, or do you still have MainWindow + main.qml?

  • the default is like this. But i just think i want to include a mainwindow class because it easy to connect with a slot and signal. As i see in qt application there should be main.cpp, mainwindow.cpp, mainwindow.h and also mainwindow.ui. So i want to use this way in qml. Correct me if i'm wrong

  • Lifetime Qt Champion

    This way you will get two windows - just because you create two of them. If you only want to use QML for your UI then there is no need for MainWindow at all. You can use signals/slots without MainWindow.

  • Moderators

    @vibolvireak What is the root Item of your QML file ?
    QQuickWidget does not support using windows as a root item. You will need to change it to Item.

  • your QQmlApplicationEngine engine; looks to be a local object which will go out of scope as soon as your constructor returns. try to make it member of your class and you will be able to see it.

  • @p3c0 said in Calling qml on mainwindow:

    @vibolvireak What is the root Item of your QML file ?
    QQuickWidget does not support using windows as a root item. You will need to change it to Item.

    You really saved my day, thanks, the QML was defined as a Window and when I tried to open in my app it always opened a new window. Now it works fine: this is a main window with a qml map inserted... it is an example about how inserting qml widgets into your app could help... Location and maps are not supported for c++

    0_1542291644528_example qquickwidget qml on c++ main window.jpg

Log in to reply