Qt 解析命令行参数

#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();
}

效果:

posted @ 2019-04-16 13:50  代萌  阅读(4752)  评论(0编辑  收藏  举报