简体   繁体   English

如何将Qt GUI应用程序的版本打印到控制台

[英]How to print version of a Qt GUI application to console

I have a GUI application written using Qt Widgets. 我有一个使用Qt小部件编写的GUI应用程序。 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. 我进行了研究,发现Qt为此提供了某种内置解决方案。

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. 如果使用-v或--version命令行开关启动此应用程序,则会显示一个包含版本信息的消息框。

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? 如何使用GUI应用程序将版本信息打印到标准控制台输出?

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 ). 查看文档( 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. 在这里( https://stackoverflow.com/a/3886128/6385043 ),您可以看到写入标准输出的可能性。 Notice the QDebug caveat. 注意QDebug警告。

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) . 您可以在构建期间使用qmake使用变量VERSION生成它,也可以将其与QApplication::setApplicationVersion(QString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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