[QT][QML] How import a simple variable from CPP class to QML View
-
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
-
@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(); }
-
-
From your code above is hard to understand your goal as you don't show the assignment of
WINDOW_DESKTOP_WIDTH
nor yourmain()
.
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);
-
@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.
-
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!
-
@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(); }