Important: Please read the Qt Code of Conduct -

Use qmake variable in cpp file

  • Hello,
    in my .pro file I specify

    VERSION = 1.0.0
    QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"

    How can I access these variables in my cpp files? I don't want to specify this information on multiple places.

  • In *pro file:

    DEFINES += MYSTRING='\\"Hello\ World\\"'

    In C++ code:

    qDebug() << MYSTRING;

  • Moderators

    @Wieland Unfortunately this is not gonna work if you want to use it in both qmake variables and defines for c++ e.g.

    QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"

    This will fail because of the spaces and you will get a weird error about a newline in constant or something similar.
    To make this work correctly I found a sorta jolly slash fest like this does the job:

    VERSION = 1.0.1
    QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"
    DEFINES += APP_VERSION=\"\\\"$${VERSION}\\\"\" \
               APP_PRODUCT=\"\\\"$${QMAKE_TARGET_PRODUCT}\\\"\" \
               APP_COMPANY=\"\\\"$${QMAKE_TARGET_COMPANY}\\\"\" \

    Note: There should be two $ next to each other but for whatever reason the forum formatting is eating it :/

    And then you can use them like text macros:

    qDebug() << APP_VERSION;
    qDebug() << APP_PRODUCT;
    qDebug() << APP_COMPANY;
    qDebug() << APP_COPYRIGHT;

  • @Chris-Kawa said:

    jolly slash fest

    Looks funny, indeed ^_^

  • Lifetime Qt Champion


    Another possibility is to generate a file e.g. a header file that contains these constants:

    INCLUDEPATH += $$OUT_PWD/include
    info_file.input = $$PWD/
    info_file.output = $$OUT_PWD/include/info.h
    QMAKE_SUBSTITUTES += info_file
    OTHER_FILES += \

    #ifndef INFO_H
    #define INFO_H
    #include <QString>
    static const QString AppVersion = QStringLiteral(\"$$APP_VERSION\");
    static const QString AppProduct = QStringLiteral(\"$$APP_PRODUCT\");
    static const QString AppCompany = QStringLiteral(\"$$APP_COMPANY\");
    static const QString AppCopyRight = QStringLiteral(\"$$APP_COPYRIGHT\");
    #endif // INFO_H

    [edit: removed double dollars workaround, renderer is now working fine SGaist]

Log in to reply