Search code examples
qtqmake

How to add a logic to .pro file based on configuration?


In my application (qmake based) I have 2 configuration, let's say CONF1 and CONF2. Each configuration defines "Additional arguments" at Project/Build settings/Build step tab:

DEFINES+=CONF1

and

DEFINES+=CONF2 

So in C++ code I can add some specified logic for specified build configuration:

#if defined CONF1
logo->setPixmap(QPixmap("conf1.png"));
#else
logo->setPixmap(QPixmap("conf2.png"));
#endif

Also I need to define icon for the application executable. So in .pro file I've added:

win32 {
    RC_ICONS = logo.ico
}

But the problem that I need different icons for different configuration.

I've tried:

contains(DEFINES, CONF1) {
    RC_ICONS = conf1.ico
}
else {
    RC_ICONS = conf2.ico
}

but that doesn't work. It looks that contains works only for variables defined inside .pro file only.

So my question - how can I add different settings (icons in my case) for different configuration?


Solution

  • As far as I'm aware qmake can't evaluate variables set in the DEFINES list, but only qmake variables.

    However, you can use a qmake variable to perform both tasks at the same time. Just assign the "conf" value to your variable, evaluate that variable to add it to the DEFINES list and then test its value using qmake functions (e.g. equals).

    As an example:

    Add the following to your additional qmake arguments (including quotes):

    "MYCONF = CONF1"
    

    Then use these directives in your .pro file:

    DEFINES += $${MYCONF}
    
    equals(MYCONF, "CONF1") {
        RC_ICONS = conf1.ico
    } else {
        RC_ICONS = conf2.ico
    }