[Solved] Constants defined in .pro file DEFINES do not work in c++ code?



  • Hello,

    I've recently started messing around with Qt5 and kind of got stuck trying to display the app version. I have found some pointers, e.g. [url=http://qt-project.org/forums/viewthread/10319]here[/url], but it's not quite working.

    This is what I have in my .pro file currently:
    @QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = TestApp
    VERSION = 0.2.0.0
    TEMPLATE = app

    Define the preprocessor macro to get the application version in our application.

    DEFINES += APP_VERSION=\"$$VERSION"
    ...
    @

    main.cpp:
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <string>
    #include <iostream>
    #include <QDebug>

    #define APP_VERSIONS "0.2.0.0"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    qDebug() << APP_VERSION;
    qDebug() << APP_VERSIONS;
    app.setApplicationVersion(APP_VERSION);
    app.setApplicationDisplayName("TestApp v" + asler.applicationVersion());
    MainWindow w;
    w.show();

    return app.exec&#40;&#41;;
    

    }
    @

    I am getting a compile error:
    @
    C2143: syntax error : missing ';' before 'constant' (Line 13)
    C2143: syntax error : missing ')' before 'constant' (Line 15)
    @

    When I hover over the two constants, QtCreator shows a tooltip of the definitions:
    @
    #define APP_VERSION "0.2.0.0"
    #define APP_VERSIONS "0.2.0.0"
    @

    So both of them are defining strings, from my point of view. Yet for some reason, the define from the .pro file refuses to work. I can neither print it out as string with qDebug(), nor set the application version.

    Any ideas?
    Building this on win64 using the 5.4.0 msvc2013 x64 toolkit


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are missing some backslashes:

    @
    DEFINES += APP_VERSION=\"$$VERSION\"
    @

    should do the trick.

    The defines goes through the command line as arguments so escaping them properly is a bit more involved.



  • Thanks for the reply!
    I forgot to mention I already experimented with more slashes, and the version you suggest shows up as
    @
    #define APP_VERSION "0.2.0.0"
    @
    with same compile errors.


  • Lifetime Qt Champion

    Juste recalled that it should be done in two phases:

    @
    VERSION = 0.2.0.0
    VERSTR = '\"$${VERSION}\"'
    DEFINES += APP_VERSION ="$${VERSTR}"
    @



  • Yes, that compiles and the version is shown, thanks! Looks kind of convoluted, but oh well.

    Another thing I noticed: despite QtCreator showing me the definition changes when I change the value in the .pro file, the version displayed in the program doesn't change.

    It seems that running qmake from the "Build" menu updates it, but somehow I have to clean the project and re-run qmake a few times for it to apply.

    What is the correct procedure to update the version displayed? Can I force it to update it automatically?


  • Lifetime Qt Champion

    You have to rebuild all files that uses the include. Currently and AFAIK, it can't be automated so your best option is to delete the build folder so it starts from scratch.



  • Ok, I can live with that I guess. Thanks for the help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.