Saturday, November 28, 2009

Qt: The best way to set the Application version

Whenever we develop any application one of the basic stuff which we need to decide is the application version number. Historically the application version number format is one of the hot topics where many lengthy wars/debates have happened.

This post is NOT about the application version number format to use, instead in this post I would be concentrating how to set and use the application version number information in a Qt project.

If this post is on any use to you, please leave a comment. :)

First way: Defining the version information within the application source code

The application version can be set as through the string constant or preprocessor Macro defined in some header file.

I have used this method many times before.

Also, here once you create the QApplication object, you can set this application version to QApplication object and ignore this constant/Macro defined from then on. Whenever you need the application version you can directly get it from qApp global QApplication object.

You can use following APIs
  • QString applicationVersion()
  • void setApplicationVersion(const QString & version)
Second way: Defining the version information within the Qt project file.

You can use Qt QMake Macro named VERSION to define the Application version.

Eg:

VERSION = 1.0

The good thing with this QMake Macro is that, it automatically creates the .rc file under Microsoft Windows and you can see the application version number from Windows Explorer by right clicking on the executable.

Eg:

AppVersion

What I do is that, I use preprocessor Macro defined through Qt project file to pass application version number to code.

Eg:

# The application version
VERSION = 1.0

# Define the preprocessor macro to get the application version in our application.
DEFINES += APP_VERSION=\\\"$$VERSION\\\"

Note: \\\" is important! Without them, the Macro is taken as a double constant instead of string constant.

Now use the Macro APP_VERSION in your code just like any other preprocessor Macro.

Eg:
    QApplication app(argc, argv);

// Setting the Application version
app.setApplicationVersion(APP_VERSION);

Similarly you can set the Application date also.

You can browse the sample Qt project @ My Google code page.

Technorati tags: , , .

12 comments:

Tony said...

Thank you for the tip. Exactly what I was wondering.

Aditya said...

Thanks for the info. Really helpful

VERSION macro should magically set the version, atleast that was what I was expecting and I didn't know what to do when applicationVersion() didn't return anything so after some searching I stumbled upon your post... Thanks again.

Aditya said...

I was expecting the VERSION macro to magically set the version

tiho_d said...

I did not like the double quotes that appear at the command line and using double quotes inside the defines does not look natural to me either. So I came with a modified version of the above approach.
Inside the Qt project files we specify:

DEFINES += APP_VERSION=$$VERSION

This will remove rhe double quotes from the command line and inside the source code we get the value as a string via a macro, i.e.:

#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)

QString appVersion = QUOTE(APP_VERSION)

Enjoy!

Raghu Nayak said...

Good work tiho_d!

Raghu Nayak said...

Earlier I had tried using
#define TO_STRING(str) #str

with APP_VERSION, but instead of converting the value of APP_VERSION to string, it used to convert APP_VERSION into string.

Your method has solved this issue, I will update the blog post with your method.

Thanks once again for posting the comment!

Demandred said...

Great way to fix this problem.

One question:
Is there a way to read this version number from a non-running executable in Linux?

Thanks for this great post!

Bartłomiej Piech said...

What about variables with spaces?
Like:
DEFINES += APP_NAME=\\\"My App\\\"

produces -DAPP_NAME=\"My -DApp\"

Anonymous said...

Thanks for this great hint! Saved me some trouble!!!

Javacoder said...

Thanks! It has helped me.

spacm said...

Great post, this matched exactly my question.

Albert said...

I recognize this is a four year old post, but for those who are still looking:

For variables with spaces, use
DEFINES += MYVAR=\"\\\"$$SOME_VAR_WITH_SPACES\\\"\"

<a href="http://www.qtcentre.org/threads/36827-qmake-generate-user-defined-header-files-or-pass-variable-definitions-to-program?p=169775#post169775>Source</a>