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. Learning QML (QtQuick 2.4)
QtWS25 Last Chance

Learning QML (QtQuick 2.4)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.0k Views
  • 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    I'm trying to learn QML, I only used the QWidget approach on my project and would like to develop the new feature with QML as it seems Qt is going this way and recommends using this for new stuff.

    I want to do a Browser with QWebEngine using QML instead of the QWidget I used.
    I add a file to my project with "Add new, QML File", then it created "QuickBrowser.qml"
    My question is this, how to I load the file and show it on screen (like .exec) when there is no .cpp file for the qml file? I'm probably missing something..`

    QML Widget

    import QtQuick 2.4
    import QtQuick.Controls 1.2
    import QtWebEngine 1.0
    import QtWebChannel 1.0
    
    ApplicationWindow {
    width: 1280
    height: 720
    visible: true
    WebEngineView {
        id: webview
        url: "http://www.qt-project.org"
        anchors.fill: parent
    }
    

    main.cpp

    int main(int argc, char *argv[]) {
     //how to load and execute my qml file like QWidget?
    }
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    JKSHJ 1 Reply Last reply
    0
    • T Offline
      T Offline
      TioRoy
      wrote on last edited by
      #2

      You can use use QmlApplicationEngine:

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

      You can check more here.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by maximus
        #3

        @TioRoy

        Thanks
        I've been checking this

        Trying to run this in my main :

         QDeclarativeView view;
         view.setSource(QUrl::fromLocalFile("QuickBrowser.qml"));
         view.show();
        
        file:///C:/Users/Calo/Desktop/MT/build-PowerVelo2-Desktop_Qt_5_4_1_MSVC2013_32bit-Release/QuickBrowser.qml: File not found
        

        If I copy the file manually where it wants:

        file:///C:/Users/Calo/Desktop/MT/build-PowerVelo2-Desktop_Qt_5_4_1_MSVC2013_32bit-    Release/QuickBrowser.qml:1:1: module "QtQuick" version 2.4 is not installed 
         import QtQuick 2.4
        

        Have to say it's a bit strange to load the file with the fileName, not with a class, my qml file is in a sub project and if I change the file path, the project will be broken? seems really hard to maintain..

        I should also mention that my project is using a standard QApplication, the mainWindow is a QWidget and not the QML type, I'm not going to change all my project so I can use the new QML approach, and so far seems too complicated to use with my project.
        I see a lot of frustration and people complaining it's hard to use QML inside classic QWidget application, I can now see why and why a lot of people are not moving to the QML recommended way.


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M maximus

          I'm trying to learn QML, I only used the QWidget approach on my project and would like to develop the new feature with QML as it seems Qt is going this way and recommends using this for new stuff.

          I want to do a Browser with QWebEngine using QML instead of the QWidget I used.
          I add a file to my project with "Add new, QML File", then it created "QuickBrowser.qml"
          My question is this, how to I load the file and show it on screen (like .exec) when there is no .cpp file for the qml file? I'm probably missing something..`

          QML Widget

          import QtQuick 2.4
          import QtQuick.Controls 1.2
          import QtWebEngine 1.0
          import QtWebChannel 1.0
          
          ApplicationWindow {
          width: 1280
          height: 720
          visible: true
          WebEngineView {
              id: webview
              url: "http://www.qt-project.org"
              anchors.fill: parent
          }
          

          main.cpp

          int main(int argc, char *argv[]) {
           //how to load and execute my qml file like QWidget?
          }
          
          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          @maximus said:

          My question is this, how to I load the file and show it on screen (like .exec) when there is no .cpp file for the qml file? I'm probably missing something..

          If you are starting a new project, the easy way is to open Qt Creator and create a Qt Quick project: "File" -> "New File or Project..." -> "Application" -> "Qt Quick Application". All the code is generated for you.

          Have to say it's a bit strange to load the file with the fileName, not with a class, my qml file is in a sub project and if I change the file path, the project will be broken? seems really hard to maintain..

          That's the same as changing the path to a .cpp file or .h file, isn't it?

          I should also mention that my project is using a standard QApplication, the mainWindow is a QWidget and not the QML type, I'm not going to change all my project so I can use the new QML approach, and so far seems too complicated to use with my project.

          Agreed; replacing everything is a bad idea.

          I see a lot of frustration and people complaining it's hard to use QML inside classic QWidget application

          It's very easy. Create a QQuickWidget, and use it to load your QML file. Then, add the QQuickWidget to your existing layout, just like any other widget.

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

          M 1 Reply Last reply
          0
          • JKSHJ JKSH

            Hi,

            @maximus said:

            My question is this, how to I load the file and show it on screen (like .exec) when there is no .cpp file for the qml file? I'm probably missing something..

            If you are starting a new project, the easy way is to open Qt Creator and create a Qt Quick project: "File" -> "New File or Project..." -> "Application" -> "Qt Quick Application". All the code is generated for you.

            Have to say it's a bit strange to load the file with the fileName, not with a class, my qml file is in a sub project and if I change the file path, the project will be broken? seems really hard to maintain..

            That's the same as changing the path to a .cpp file or .h file, isn't it?

            I should also mention that my project is using a standard QApplication, the mainWindow is a QWidget and not the QML type, I'm not going to change all my project so I can use the new QML approach, and so far seems too complicated to use with my project.

            Agreed; replacing everything is a bad idea.

            I see a lot of frustration and people complaining it's hard to use QML inside classic QWidget application

            It's very easy. Create a QQuickWidget, and use it to load your QML file. Then, add the QQuickWidget to your existing layout, just like any other widget.

            M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            @JKSH said:

            It's very easy. Create a QQuickWidget, and use it to load your QML file. Then, add the QQuickWidget to your existing layout, just like any other widget.

            Thanks JKSH, will try the QQuickWidget approach instead. I guess I will have to bundle the .qml in resource file or something? As for the file name, you are right it's like changing the .cpp name. I just would have liked that the .qml to deploy automatically and not have to worry about putting it in resource, that adds another step that was not present with QWidget.

            I have already done a browser using a QWidget but would like to learn QtQuick since I heard in the Qt conference that it's the future way of doing stuff. As for myself, I prefer UI done with Jquery/bootstrap etc., all good open-source frameworks, not sure why they redo the wheel with QML.

            Have a great day,
            Max


            Free Indoor Cycling Software - https://maximumtrainer.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