Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Constants defined in .pro file DEFINES do not work in c++ code?

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    soukyuu
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        soukyuu
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Juste recalled that it should be done in two phases:

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            soukyuu
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • S Offline
                S Offline
                soukyuu
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved