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.



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.



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


# The application version

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

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.

    QApplication app(argc, argv);

// Setting the Application version

Similarly you can set the Application date also.

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

Technorati tags: , , .