different build with Configure commandline macro
-
wrote on 15 Apr 2024, 02:48 last edited by
Hello All.
I want to create two builds by passing some configure options in command line and add the macro in qml , so that based on that specific build will pick colors and ICON source. Basically, it should be like if I execute a certain command line for build it should fetch the specific color and ICON from qml and application should show the same. its urgent, kindly give some ideas or example programs/ links. -
Hello All.
I want to create two builds by passing some configure options in command line and add the macro in qml , so that based on that specific build will pick colors and ICON source. Basically, it should be like if I execute a certain command line for build it should fetch the specific color and ICON from qml and application should show the same. its urgent, kindly give some ideas or example programs/ links.There are multiple ways to do this, I'll just pick one to keep this answer simple.
In cmake:
option(SOME_OPTION "Enables different colors" OFF) if (SOME_OPTION) add_definitions(-DSOME_OPTION) endif()
Then in c++:
#ifdef SOME_OPTION qmlEngine->rootContext()->setContextProperty("someOption", true); #else qmlEngine->rootContext()->setContextProperty("someOption", false); #endif
This can be improved a lot by using some controller object (a subclass of QObject, maybe a QML_SINGLETON) which will provide good colors and icons to QML based on value of SOME_OPTION). But here, to keep it simple, I'm just passing the property
someOption
.Lastly, in QML:
Rectangle { color: someOption ? "#ff0000" : "#00ff00" }
With code prepared like that, you can produce different-looking builds like so:
cmake -S /path/to/source/code -B /build/path -DSOME_OPTION=On cmake --build /build/path --config Release
Just set the option to
On
orOff
, recompile, and you will get different builds. -
Hello All.
I want to create two builds by passing some configure options in command line and add the macro in qml , so that based on that specific build will pick colors and ICON source. Basically, it should be like if I execute a certain command line for build it should fetch the specific color and ICON from qml and application should show the same. its urgent, kindly give some ideas or example programs/ links.The bad news is: QML doesn't know preprocessor macros.
The good news is: There are easy workarounds.
I guess your project has amain.cpp
launchning the QML engine.Suggestion (just from the top of my head, without trying to compile):
Add e.g.-DMYFLAG
to the configure arguments.
Inmain.cpp
set a context property, based on the macro:#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> /* any other includes needed */ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; #ifdef MYFLAG const bool myFlag = true; #elseif const bool myFlag = false; #endif engine.rootContext()->setContextProperty("myFlag", QVariant(myFlag)); const QUrl url(":/path/to/Main.qml"); engine.load(url); return app.exec();
Evaluate the property in QML:
ApplicationWindow { Component.onCompleted { if (myFlag) { // do something } } }
-
The bad news is: QML doesn't know preprocessor macros.
The good news is: There are easy workarounds.
I guess your project has amain.cpp
launchning the QML engine.Suggestion (just from the top of my head, without trying to compile):
Add e.g.-DMYFLAG
to the configure arguments.
Inmain.cpp
set a context property, based on the macro:#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> /* any other includes needed */ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; #ifdef MYFLAG const bool myFlag = true; #elseif const bool myFlag = false; #endif engine.rootContext()->setContextProperty("myFlag", QVariant(myFlag)); const QUrl url(":/path/to/Main.qml"); engine.load(url); return app.exec();
Evaluate the property in QML:
ApplicationWindow { Component.onCompleted { if (myFlag) { // do something } } }
@sierdzio
Simple minds not only think alike, but also post at the same time :-)
But I must admit, knowing color hex codes by heart exceeds my level!
1/4