码迷,mamicode.com
首页 > 其他好文 > 详细

Qt 解析命令行参数

时间:2019-04-16 14:36:40      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:comm   alt   lis   说明   add   图片   default   mode   table   

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QCommandLineParser>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QGuiApplication::setApplicationName("Qt");  // 应用名称
    QGuiApplication::setApplicationVersion("0.1");  // 应用版本号

    QCommandLineParser parser;
    parser.setApplicationDescription(QGuiApplication::translate("main", "Qt"));  // 设置应用程序描述信息

    parser.addHelpOption();  // 添加帮助选项 ("-h" 或 "--help")
    parser.addVersionOption();  // 添加版本选项 ("-v" 或 "--version") 

    parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);  // 举例说明:将 "-adb" 当成一个选项来看,而不是看成 "-a -b -c"

//    parser.addPositionalArgument("xxx", QGuiApplication::translate("main", "?????? undefined"));

    QCommandLineOption widthOption(QStringList() << "wid" << "width",
                                   QGuiApplication::translate("main", "Width of the covered area (default is 800)."),
                                   QGuiApplication::translate("main", "width"), "800");
    parser.addOption(widthOption);

    QCommandLineOption heightOption(QStringList() << "hei" << "height",
                                    QGuiApplication::translate("main", "Height of the covered area (default is 480)."),
                                    QGuiApplication::translate("main", "height"), "480");
    parser.addOption(heightOption);

    QCommandLineOption xOption(QStringList() << "x",
                               QGuiApplication::translate("main", "The x coordinate of the covered area (default is 0)."),
                               QGuiApplication::translate("main", "x"), "0");
    parser.addOption(xOption);

    QCommandLineOption yOption(QStringList() << "y",
                               QGuiApplication::translate("main", "The y coordinate of the covered area (default is 0)."),
                               QGuiApplication::translate("main", "y"), "0");
    parser.addOption(yOption);

    QCommandLineOption colorOption(QStringList() << "c" << "color",
                               QGuiApplication::translate("main", "The color of the covered area (default is black)."),
                               QGuiApplication::translate("main", "color"), "black");
    parser.addOption(colorOption);

    parser.process(app);

//    const QStringList args = parser.positionalArguments();

    int width = parser.value(widthOption).toInt();
    int height = parser.value(heightOption).toInt();
    if (0 > width || 0 > height) {
        fprintf(stderr, "%s\n", qPrintable(QGuiApplication::translate("main", "Error: Invalid format argument. "
                                                                              "Width and height must be greater than 0.")));
        parser.showHelp(1);
    }
    int x = parser.value(xOption).toInt();
    int y = parser.value(yOption).toInt();
    QString color = parser.value(colorOption);

    QQuickView view;
    view.setGeometry(x, y, width, height);
    view.setColor(QColor(color));
    view.setFlags(Qt::FramelessWindowHint);
//    view.setSource(QUrl("qrc:/main.qml"));
    view.show();

    return app.exec();
}

效果:

技术图片

Qt 解析命令行参数

标签:comm   alt   lis   说明   add   图片   default   mode   table   

原文地址:https://www.cnblogs.com/adorkable/p/10716580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!