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. different build with Configure commandline macro
Forum Updated to NodeBB v4.3 + New Features

different build with Configure commandline macro

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 226 Views 2 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.
  • A Offline
    A Offline
    Amarjit123
    wrote on last edited by
    #1

    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.

    sierdzioS Axel SpoerlA 2 Replies Last reply
    0
    • A Amarjit123

      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.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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 or Off, recompile, and you will get different builds.

      (Z(:^

      1 Reply Last reply
      1
      • A Amarjit123

        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.

        Axel SpoerlA Online
        Axel SpoerlA Online
        Axel Spoerl
        Moderators
        wrote on last edited by
        #3

        The bad news is: QML doesn't know preprocessor macros.
        The good news is: There are easy workarounds.
        I guess your project has a main.cpplaunchning the QML engine.

        Suggestion (just from the top of my head, without trying to compile):
        Add e.g. -DMYFLAGto the configure arguments.
        In main.cppset 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
                }
            }
        }
        

        Software Engineer
        The Qt Company, Oslo

        Axel SpoerlA 1 Reply Last reply
        0
        • Axel SpoerlA Axel Spoerl

          The bad news is: QML doesn't know preprocessor macros.
          The good news is: There are easy workarounds.
          I guess your project has a main.cpplaunchning the QML engine.

          Suggestion (just from the top of my head, without trying to compile):
          Add e.g. -DMYFLAGto the configure arguments.
          In main.cppset 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
                  }
              }
          }
          
          Axel SpoerlA Online
          Axel SpoerlA Online
          Axel Spoerl
          Moderators
          wrote on last edited by Axel Spoerl
          #4

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

          Software Engineer
          The Qt Company, Oslo

          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