Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

preprocessing in QML?



  • Hi,
    is there any ways to use #ifdef on macros defined in the pro file?
    so to have a single QML file that could have variants depending on the conf.
    I'm thinking on a simple example: easily change my Items from a background image to a simple colour or gradient.


  • Qt Champions 2019

    @mbruel As far as I know not. But you could use different QML files depending on the condition.



  • it's to avoid to duplicate code...
    maybe in JS it's possible to do something similar using a global variable (from JS or C++) but probably more an hassle than a simple #ifdef


  • Moderators

    @mbruel last thing I heard, was that we get something along those lines with Qt6 🤷‍♂️



  • @J-Hilk
    that would be great. Preprocessing statements are really a strength of C++ over all the over languages (even if you can find some stuff in java but not native).
    it allows to maintain one single main production branch that can easily be tweaked depending on the client, device or even just you mood :p



  • I do this for QT_DEBUG:

            #ifdef QT_DEBUG
            engine->rootContext()->setContextProperty("QT_DEBUG", true);
            #else
            engine->rootContext()->setContextProperty("QT_DEBUG", false);
            #endif
    

    I use this to change the behavior and hide dev tools from the end user. It is not automatic though.



  • @fcarney
    how do you access your "QT_DEBUG" variable in QML or JS?
    what I'm asking for would be to <define>/<don't define> Items using some kind of preprocessing, not later in JavaScript using the visible property. Do you see what I mean? It's much more efficient as there are no dynamic test, it would be "pre-compiled"... or just escaping for an interpreted language.



  • Use a Loader
    I see what you mean. There is nothing to define except maybe some kind of loading/dynamic JS.
    You just use QT_DEBUG as a variable in your code. It is a property available globally.



  • This post is deleted!

  • Qt Champions 2018

    Not exactly the same but you could use QQmlFileSelector to load different files depending on some condition.

    You would have a Foobar.qml loaded normally but in debug mode +debug/Foorbar.qml would be loaded instead.


Log in to reply