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

效果:

转载于:https://www.cnblogs.com/adorkable/p/10716580.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值