qt实现命令行查询程序相关信息QCommandLineParser,QCommandLineOption的使用

我们知道,程序是可以通过命令行打开的,做法是打开运行cmd,打开命令行窗口,然后输入程序的完整路径就可以打开程序,比如在我的电脑路径F:\QTCode\TestCode\QtUseOpenGlTest\build-hellogl2-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug下有个hellogl2.exe程序,通过命令行打开方式如下:

按下win+R——输入cmd——按回车出现如下界面: 

 然后输入F:\QTCode\TestCode\QtUseOpenGlTest\build-hellogl2-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\hellogl2.exe,程序就运行起来了

 那么如果我想要在运行程序之前查看一下这个程序的相关信息怎么办呢?

我们可以通过qt的QCommandLineOption类来给程序添加一些命令行可以查看的信息,代码如下:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QCoreApplication::setApplicationName("Qt Hello GL 2 Example");
    QCoreApplication::setOrganizationName("QtProject");
    QCoreApplication::setApplicationVersion(QT_VERSION_STR);
    QCommandLineParser parser;
    parser.setApplicationDescription(QCoreApplication::applicationName());
    parser.addHelpOption();
    parser.addVersionOption();
    QCommandLineOption multipleSampleOption("multisample", "Multisampling");
    parser.addOption(multipleSampleOption);
    QCommandLineOption coreProfileOption("coreprofile", "Use core profile");
    parser.addOption(coreProfileOption);
    QCommandLineOption transparentOption("transparent", "Transparent window");
    parser.addOption(transparentOption);

    parser.process(app);

    QSurfaceFormat fmt;
    fmt.setDepthBufferSize(24);
    if (parser.isSet(multipleSampleOption))
        fmt.setSamples(4);
    if (parser.isSet(coreProfileOption)) {
        fmt.setVersion(3, 2);
        fmt.setProfile(QSurfaceFormat::CoreProfile);
    }
    QSurfaceFormat::setDefaultFormat(fmt);
    //TODO:程序主窗口显示相关代码
}

然后在命令行中查看相关信息的方式如下:

 输入F:\QTCode\TestCode\QtUseOpenGlTest\build-hellogl2-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\hellogl2.exe -h,就自动会跳出一个界面,显示帮助信息,其他还可以输入-v  --multisample --transparent等

Guess you like

Origin blog.csdn.net/weixin_43935474/article/details/118966630