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. [QT][QML] How import a simple variable from CPP class to QML View

[QT][QML] How import a simple variable from CPP class to QML View

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 264 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.
  • S Offline
    S Offline
    Shadow.01
    wrote on last edited by Shadow.01
    #1

    Hi all,

    in my QT6 project (created with CMake / OS Ubuntu 20.04) I have a UIConfig file with some global configuration const for the entire app (ground basic color, window size, welcome message and so on). I tried to use it in my QML view, but I see an error. Below all detail. How can I solve the problem? Thank you very much!

    UIConfig header:

    #ifndef UICONFIG_H
    #define UICONFIG_H
    
    #include <QQuickPaintedItem>
    #include <QObject>
    
    class UIConfig
    {
        QML_ELEMENT
    
    public:
        static double const FOOBAR;
        static int const WINDOW_DESKTOP_WIDTH;
        static int const WINDOW_DESKTOP_HEIGHT;
    
    
    };
    
    #endif // UICONFIG_H
    

    UIConfig cpp:

    #include "uiconfig.h"
    
    double const UIConfig::FOOBAR = 42;
    int const UIConfig::WINDOW_DESKTOP_WIDTH = 1200;
    int const UIConfig::WINDOW_DESKTOP_HEIGHT = 800;
    

    A fragment of my QML view:

    import QtQuick 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.5
    import com.my.app
    
    Window {
        width: WINDOW_DESKTOP_WIDTH
        height: 800
        visible: true
        title: qsTr("Container page")
        color: "#ffffff"
    
        Component.onCompleted: {
    
            x = Screen.width / 2 - width / 2
            y = Screen.height / 2 - height / 2
        }
    (...)
    

    CMakeLists file:

    cmake_minimum_required(VERSION 3.14)
    
    project(myapp VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(QT NAMES Qt6 COMPONENTS Core Quick REQUIRED)
    find_package(Qt6 COMPONENTS Core Quick REQUIRED)
    
    set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
    
    set(PROJECT_SOURCES
            main.cpp
            qml.qrc
            images.qrc
            global/uiconfig.h
            global/uiconfig.cpp
    )
    
    qt_add_executable(myapp
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
    
    target_compile_definitions(myapp
      PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    target_link_libraries(myapp
      PRIVATE Qt6::Core Qt6::Quick)
    
    set_target_properties(myapp PROPERTIES
        QT_QML_MODULE_VERSION 1.0
        QT_QML_MODULE_URI com.my.myapp
    )
    
    list(APPEND QML_IMPORT_PATH .)
    
    qt6_qml_type_registration(myapp)
    qt_import_qml_plugins(myapp)
    qt_finalize_executable(myapp)
    

    Runtime error if I try to import a var, i.e. WINDOW_DESKTOP_WIDTH assign to window width:

    ReferenceError: WINDOW_DESKTOP_WIDTH is not defined
    
    1 Reply Last reply
    0
    • S Shadow.01

      @VRonin you right, sorry. I edited the question. As you can see now, the way I want use this var is by setting as a width of my mentioned qml window the WINDOW_DESKTOP_WIDTH value defined in UIConfig (see please section "A fragment of my QML view:".

      Now, in which part of my app should I add the row you wrote? In the main.cpp? Thanks again for your helping.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #7

      @Shadow-01 said in [QT][QML] How import a simple variable from CPP class to QML View:

      in which part of my app should I add the row you wrote? In the main.cpp?

      yes, you should have something similar to:

      #include "uiconfig.h"
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          engine.rootContext()->setContextProperty("WINDOW_DESKTOP_WIDTH", &UIConfig::WINDOW_DESKTOP_WIDTH);
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
          return app.exec();
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        See https://qmlbook.github.io/ch18-extensions/extensions.html#

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Shadow.01
          wrote on last edited by
          #3

          Doesn't work and it is totally different from my case. I am not opening a QML from a CPP class, but from other QML. I simply want a const/var, I can define in a certain point of my app, and recall anywhere. Other ideas? Thank you very much.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4

            From your code above is hard to understand your goal as you don't show the assignment of WINDOW_DESKTOP_WIDTH nor your main().
            What I understood is that you have a variable defined in C++ and you want to use it in QML and that's what that section of the QML book covers

            //QQmlApplicationEngine engine;
            engine.rootContext()->setContextProperty("WINDOW_DESKTOP_WIDTH", &UIConfig::WINDOW_DESKTOP_WIDTH);
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply
            1
            • VRoninV VRonin

              From your code above is hard to understand your goal as you don't show the assignment of WINDOW_DESKTOP_WIDTH nor your main().
              What I understood is that you have a variable defined in C++ and you want to use it in QML and that's what that section of the QML book covers

              //QQmlApplicationEngine engine;
              engine.rootContext()->setContextProperty("WINDOW_DESKTOP_WIDTH", &UIConfig::WINDOW_DESKTOP_WIDTH);
              
              S Offline
              S Offline
              Shadow.01
              wrote on last edited by
              #5

              @VRonin you right, sorry. I edited the question. As you can see now, the way I want use this var is by setting as a width of my mentioned qml window the WINDOW_DESKTOP_WIDTH value defined in UIConfig (see please section "A fragment of my QML view:".

              Now, in which part of my app should I add the row you wrote? In the main.cpp? Thanks again for your helping.

              VRoninV 1 Reply Last reply
              0
              • S Offline
                S Offline
                Shadow.01
                wrote on last edited by Shadow.01
                #6

                I DID IT!

                Thanks for helping.

                The way was, put the variable in the cpp and h files mentioned in my original question (it was correct); put your row in the main.cpp file; call the global path of the app in the qml file and call the desired var in the qml code.

                I didn't see before, because in your link was mentioned a specific cpp file for a qml view; in my case, some view are not linked to a cpp file. Thank you so much, you given me a big help!

                1 Reply Last reply
                0
                • S Shadow.01

                  @VRonin you right, sorry. I edited the question. As you can see now, the way I want use this var is by setting as a width of my mentioned qml window the WINDOW_DESKTOP_WIDTH value defined in UIConfig (see please section "A fragment of my QML view:".

                  Now, in which part of my app should I add the row you wrote? In the main.cpp? Thanks again for your helping.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #7

                  @Shadow-01 said in [QT][QML] How import a simple variable from CPP class to QML View:

                  in which part of my app should I add the row you wrote? In the main.cpp?

                  yes, you should have something similar to:

                  #include "uiconfig.h"
                  int main(int argc, char *argv[])
                  {
                      QGuiApplication app(argc, argv);
                      QQmlApplicationEngine engine;
                      engine.rootContext()->setContextProperty("WINDOW_DESKTOP_WIDTH", &UIConfig::WINDOW_DESKTOP_WIDTH);
                      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                      if (engine.rootObjects().isEmpty())
                          return -1;
                      return app.exec();
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved