There is no standard way of getting version information from applications on Linux systems. Often a --version
command line switch is provided for this purpose. But there's no freedesktop.org standard that would provide a more descriptive version information. The .desktop
files don't contain such information - the version there is the file format version, not the application version.
So what you're seeking is simply not implemented. But if the --version
was a sufficient interface, then here's how one could do it.
Your .pro
file needs to contain both the version setting and pass the version to the C/C++ compiler:
VERSION = 1.0.0
DEFINES += VERSION_STRING=\\\"$${VERSION}\\\"
The version can be then shown from the command line by passing the --version
argument. You can leverage the command line parser, available since Qt 5.2:
#include <QCoreApplication>
#include <QCommandLineParser>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
a.setApplicationName("version-cmd");
a.setApplicationVersion(VERSION_STRING);
QCommandLineParser parser;
parser.addVersionOption();
parser.process(a);
return a.exec();
}
The output:
$ version-cmd --version
version-cmd 1.0.0
$
And if you don't use at least Qt 5.2, it's easy enough to check if a.args().contains("--version")
and act on that.