0

I have a GUI application written using Qt Widgets. I've added versioning and I'm planning to write an update manager too. In order this to work the update manager must be able to determine the version of my app. I thought of implementing this by running my app with a version switch then parsing it's output. I did a research and I found out that Qt has some kind of built in solution for this.

Here is an example:

#include "mainwindow.h"
#include <QApplication>
#include <QCommandLineParser>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QApplication::setApplicationVersion("1.0.0");

    QCommandLineParser parser;
    auto versionOption = parser.addVersionOption();
    parser.process(app);

    if (parser.isSet(versionOption))
    {
        MainWindow w;
        w.show();
        return app.exec();
    }

    return 0;
}

If I launch this app with a -v or --version command line switch, I get a message box containing the version information.

I need to achieve the same, only the information should be printed to standard output. If the app is launched with the version switch it should only display the version in the console then close.

How could I print the version information to the standard console output with a GUI app?

Szőke Szabolcs
  • 471
  • 7
  • 17
  • What about the Qt Installer Framework? It shippes the deployed application with update capabilities like you know it from the Qt Installation. https://doc.qt.io/qtinstallerframework/index.html – maxik Jun 17 '16 at 13:18
  • I would rather implement a simple solution then add complexity than using a ready made one and trying to figure out the reason behind the code. – Szőke Szabolcs Jun 17 '16 at 13:20
  • Good, your choice. Then why not use a simple text file with contains the current version residing in the application directory. – maxik Jun 17 '16 at 13:21
  • That could be a solution too, and I will be using it if my original question remains unanswered. – Szőke Szabolcs Jun 17 '16 at 13:25

1 Answers1

2

As we cleared some points in comments let's move on. ;)

Take a look at the documentation (http://doc.qt.io/qt-5/qapplication.html#details). In the detail section you see a sane way how to properly parse and handle command line options.

And here (https://stackoverflow.com/a/3886128/6385043) you can see a possibility for writing to standard output. Notice the QDebug caveat.

In my opinion, stick to the text file. You may generate it during build with qmake using the variable VERSION, which you can also use with QApplication::setApplicationVersion(QString).

Community
  • 1
  • 1
maxik
  • 987
  • 12
  • 33