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)
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:
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: OpenGuru.org weblog, Qt, Application version.
12 comments:
Thank you for the tip. Exactly what I was wondering.
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.
I was expecting the VERSION macro to magically set the version
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!
Good work tiho_d!
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!
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!
What about variables with spaces?
Like:
DEFINES += APP_NAME=\\\"My App\\\"
produces -DAPP_NAME=\"My -DApp\"
Thanks for this great hint! Saved me some trouble!!!
Thanks! It has helped me.
Great post, this matched exactly my question.
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>
Post a Comment