[Qt-interest] application version and DRY principle
Constantin Makshin
cmakshin at gmail.com
Fri Feb 18 15:44:09 CET 2011
I usually create "version.pri" file (with correct values instead of "x", "y" and "z", of course):
VERSION_MAJOR = x
VERSION_MINOR = y
VERSION_PATCH = z
VERSION = $${VERSION_MAJOR}.$${VERSION_MINOR}.$${VERSION_PATCH}
Writing version information into a separate file instead of the primary project file makes changing version number (and extracting it in packaging scripts) a bit easier and more convenient. Then I add this to the .pro:
include(version.pri)
DEFINES += \
VERSION_MAJOR=$$VERSION_MAJOR \
VERSION_MINOR=$$VERSION_MINOR \
VERSION_PATCH=$$VERSION_PATCH \
VERSION_STR=\\\"$$VERSION\\\"
And in "main.cpp", soon after creating application object:
app.setApplicationVersion(QLatin1String(VERSION_STR));
If one of the target platforms is Symbian, I write things this way:
.pro:
include(version.pri)
DEFINES += \
VERSION_MAJOR=$$VERSION_MAJOR \
VERSION_MINOR=$$VERSION_MINOR \
VERSION_PATCH=$$VERSION_PATCH
!symbian: DEFINES += VERSION_STR=\\\"$$VERSION\\\"
main.cpp:
#if defined(VERSION_STR)
app.setApplicationVersion(QLatin1String(VERSION_STR));
#else
app.setApplicationVersion(QString("%1.%2.%3").arg(VERSION_MAJOR).arg(VERSION_MINOR).arg(VERSION_PATCH));
#endif
That's because I couldn't find a way to define a text macro in Symbian toolchain.
"About" window is shown this way (with "<...>" placeholders replaced by actual information):
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("About <application name>"));
msgBox.setIconPixmap(qApp->windowIcon().pixmap(64));
msgBox.setText(tr("<application name>\nVersion %1").arg(qApp->applicationVersion()));
msgBox.setInformativeText(tr("<some details about copyrights and licensing>"));
msgBox.exec();
On Friday 18 February 2011 15:18:49 Dmitry Teslenko wrote:
> On Fri, Feb 18, 2011 at 14:50, Konrad Rosenbaum <konrad at silmor.de> wrote:
> > On Fri, February 18, 2011 12:20, Dmitry Teslenko wrote:
> >> How to define application version once and use it everywhere:
> >> a) in *.pro file to set custom package name like Program-<version>
> >
> > Have a look at the DEFINES variable of qmake (there is quite an extensive
> > reference for qmake in the Qt docu).
> >
> >> b) in c++ sources to pass it to QApplication::setVersionNumber and
> >> have about dialog with version number
> >
> > DEFINES get passed to the compiler, so you can put any kind of macro
> > inside, for example
> >
> > DEFINES += MYAPPVERSION="1.2.3 beta build 234"
>
> Did you use it for yourself or you just heard of defines?
>
> The way you suggest it me I'll get this makefile:
>
> DEFINES = -DMYAPPVERSION=1.2.3 beta build 234 -DSOMETHING_ELSE
>
> That's unusable makefile.
> If you add additional quotes like this:
>
> DEFINES += MYAPPVERSION="\"1.2.3 beta build 234\""
>
> You'll get right makefile. But you'll get syntax error in source code
> because when you type something like this:
>
> QApplication::setApplicationVersion(MYAPPVERSION);
>
> Compliler will get:
>
> QApplication::setApplicationVersion(1.2.3 beta build 234);
>
> and will complain. And he would be right.
>
>
> > You probably meant QCoreApplication::setApplicationVersion(QString) - right?
>
> Yes, you are right. Thanks.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110218/26963999/attachment.bin
More information about the Qt-interest-old
mailing list