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

时间: 2024-08-29 16:53:57

Qt 解析命令行参数的相关文章

Qt - 解析命令行

Qt从5.2版开始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行参数. 一.命令行写法 命令行:"-abc" 在QCommandLineParser的解析模式为ParseAsCompactedShortOptions(默认)时会被认为是3个参数,即"-a"."-b"和"-c" QCommandLineOption op1("a"); QCommand

Qt之命令行参数

简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设置命令行参数Arguments,来设置需要用到的信息.也可以用来测试其它进程传参功能. 简述 设置参数 解析 更多参考 设置参数 选择:项目 -> 构建和运行 -> 运行,在Arguments输入框中输入需要传递的参数. 为了演示,我传递了一个Json对象:{\"UserName\&qu

boost之program_options库,解析命令行参数、读取配置文件

一.命令行解析 tprogram_options解析命令行参数示例代码: [cpp] view plaincopy #include <iostream> using namespace std; #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char*argv[]) { //int level; po::options_descripti

flag 是Go 标准库提供的解析命令行参数的包QANDA.REN文库

flag flag 是Go 标准库提供的解析命令行参数的包. 使用方式: flag.Type(name, defValue, usage) 其中Type为String, Int, Bool等:并返回一个相应类型的指针. flag.TypeVar(&flagvar, name, defValue, usage) 将flag绑定到一个变量上. 自定义flag 只要实现flag.Value接口即可: type Value interface { String() string Set(string)

用Google的gflags轻松的编码解析命令行参数

支持的参数类型 gflags支持的类型有bool,int32,int64,uint64,double和string.可以说这些基本类型大体上满足了我们的需求. DEFINE_bool: boolean DEFINE_int32: 32-bit integer DEFINE_int64: 64-bit integer DEFINE_uint64: unsigned 64-bit integer DEFINE_double: double DEFINE_string: C++ string 比如上文

解析命令行参数的方法

一.关于解析命令行参数的方法 关于"解析命令行参数"的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考:https://www.cnblogs.com/aland-1415/p/6613449.html 这里为大家介绍一个比sys.argv更强大的optparse模块. 这里说一句题外话,点开optparse的源码,第一行注释是这样的:A powerful, extensible, and

3.QT中QCommandLineParser和QCommandLineOption解析命令行参数

 1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include <QDebug> #include <stdio.h> int main(int argc, char** argv) { QCoreApplication app(argc, argv); app.setApplicationVersion("1.0.0.0");

Python3-argparse模块-解析命令行参数

官方文档 http://python.usyiyi.cn/translate/python_352/library/argparse.html 代码示例 import argparse # 1.获取参数解析对象 parser = argparse.ArgumentParser(description="帮助信息前的一些描述信息,可以不写哦") # 2.添加可解析的参数 # add_argument()函数常用参数 # name or flags 选项字符串的名字或列表,例如foo或者

getopt_long函数解析命令行参数

转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用getopt来实现的. 在Linux下使用getopt写程序是一种比较cool的事情,下面来简单的介绍一下getopt的使用. === getopt使用 === 在讨论参数处理之前,我们先明确两个概念:选项.选项参数gcc -g -o test test.c我们经常使用上面的命令来编译程序,这里g和