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. Calling qml on mainwindow

Calling qml on mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 7.1k 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.
  • V Offline
    V Offline
    vibolvireak
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      1 Reply Last reply
      1
      • V Offline
        V Offline
        vibolvireak
        wrote on last edited by
        #3

        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.
        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vibolvireak
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vibolvireak
                wrote on last edited by
                #7

                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

                p3c0P 1 Reply Last reply
                0
                • jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • V vibolvireak

                    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

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

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

                    157

                    JoeBermejalesJ 1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      prat22
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • p3c0P p3c0

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

                        JoeBermejalesJ Offline
                        JoeBermejalesJ Offline
                        JoeBermejales
                        wrote on last edited by JoeBermejales
                        #11

                        @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

                        http://reparacionplotter.com

                        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