19

I am using Qt 4.5 in Windows XP. My pro file has the variable VERSION = 1.0. Now i need to read this variable and get its value (1.0) from the source code. So that I don't have to maintain another variable for version inside my source code. I just read from the pro file and update it. So that the value remains consistent all over my project. Is it possible? Any pointers regarding this are welcome..

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
liaK
  • 11,422
  • 11
  • 48
  • 73

3 Answers3

21

Use somethings like this:

DEFINES += VERSION=\\\"$$VERSION\\\"

This will define a macro that you can use in C source code. Get rid of the backslashes and quotes if you want a number, not a string.

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
8

I'll elaborate on this a bit.

In the YourApp.pro:

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

In the main.cpp:

#include <QApplication>
QCoreApplication::setApplicationVersion(QString(APP_VERSION));

Wherever else in your sources, e.g. in the imaginary controller.cpp:

#include <QApplication>
QString yourAppVersion = QCoreApplication::applicationVersion();
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
0

somehow, when I tried qDebug() << QString(APP_VERSION); in a class.cpp not in main.cpp. has an error "C2065" APP_VERSION: undeclared identifier". but when I tried in main.cpp that worked.