qtqt5qt-creatorqbs

How to access Qt Build System project (QBS) variables in Qt code


Good day.

I need to access specific QBS variables inside my Qt code.

An example is the Application's name, organisation or even the flavour, all variables specified like this in my application qbs file.

import qbs

Project {

    // These variables should be available in C++ code.
    name: "my_app_name"
    organization: "Organisation_Name"
    flavour:"AppFlavour"
    minimumQbsVersion: "1.7.1"

    CppApplication {
        files: [
        ]
        Depends { name: "Qt"; submodules: ['core', 'network'] }

        cpp.cxxLanguageVersion: "c++11"

        cpp.defines: [
            "QT_DEPRECATED_WARNINGS",
        ]

        consoleApplication: true

        Group {
            name: "source"
            files: [
                "qconfigurationmanager.cpp",
            ]
        }

        Group {
            name: "header"
            files: [
                "qconfigurationmanager.h",
            ]
        }

        Group {     // Properties for the produced executable
            fileTagsFilter: "application"
            qbs.install: true
        }
    }
}

Looking at the Qt documentation for QBS, I did not find any reference to using QBS variables in Qt code.

This is the only link of using QBS variables, but only within the QBS file

I would like to do this:

QString appflavour = Qbs.get("flavour")

How can I do this?


Solution

  • A possible option is to use a DEFINES and obtain the data through a macro:

    import qbs
    
    Project {
        minimumQbsVersion: "1.7.1"
    
        property string name: "my_app_name"
        property string organization: "Organisation_Name"
        property string flavour:"AppFlavour"
    
        CppApplication {
            Depends { name: "Qt"; submodules: ['core', 'network']}
            cpp.cxxLanguageVersion: "c++11"
            consoleApplication: true
    
            cpp.defines: [
                "QT_DEPRECATED_WARNINGS",
                "name=" + project.name,
                "organization=" +  project.organization,
                "flavour=" + project.flavour
            ]
    ...
    

    #define QUOTE_(x) #x
    #define QUOTE(x) QUOTE_(x)
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QString appname = QUOTE(name);
        QString organization = QUOTE(organization);
        QString appflavour = QUOTE(flavour);
    
        qDebug()<< appname << organization << appflavour;
    
    ...
    

    Output:

    "my_app_name" "Organisation_Name" "AppFlavour"