Calling qml on mainwindow



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

    mainwindow.h

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

    mainwindow.cpp

    #include "mainwindow.h"
    #include "qqmlapplicationengine.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    }
    
    

    main.cpp

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

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


  • Moderators

    Hi,
    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.
    Note***

    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.

  • Moderators

    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..


  • Moderators

    Here you can see an example: http://doc.qt.io/qt-5/qqmlapplicationengine.html

    #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


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.