简体   繁体   中英

How to print version of a Qt GUI application to console

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?

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM