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"), "");
parser.addOption(widthOption); QCommandLineOption heightOption(QStringList() << "hei" << "height",
QGuiApplication::translate("main", "Height of the covered area (default is 480)."),
QGuiApplication::translate("main", "height"), "");
parser.addOption(heightOption); QCommandLineOption xOption(QStringList() << "x",
QGuiApplication::translate("main", "The x coordinate of the covered area (default is 0)."),
QGuiApplication::translate("main", "x"), "");
parser.addOption(xOption); QCommandLineOption yOption(QStringList() << "y",
QGuiApplication::translate("main", "The y coordinate of the covered area (default is 0)."),
QGuiApplication::translate("main", "y"), "");
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 ( > width || > height) {
fprintf(stderr, "%s\n", qPrintable(QGuiApplication::translate("main", "Error: Invalid format argument. "
"Width and height must be greater than 0.")));
parser.showHelp();
}
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 解析命令行参数的更多相关文章
- Qt之命令行参数
简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设 ...
- boost之program_options库,解析命令行参数、读取配置文件
一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...
- optparse模块解析命令行参数的说明及优化
一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...
- python解析命令行参数
常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...
- linux 中解析命令行参数(getopt_long用法)
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...
- C语言中使用库函数解析命令行参数
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...
- Windows下解析命令行参数
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...
- 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
1 新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...
- QT解析命令行(QCommandLineOption和QCommandLineParser类)
Qt从5.2版开始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行参数. 一.命令行写法命令行:"-abc" 在QComma ...
随机推荐
- jdk 1.8.0_131 Class JavaLaunchHelper is implemented
错误提示:objc[49447]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jd ...
- Redis 数据库使用和搭建
1.redis中文网 http://www.redis.cn/documentation.html 2.数据类型介绍 http://redis.cn/topics/data-types-intro.h ...
- 解决pjax重复加载js导致事件重复绑定的问题
个人博客 地址:http://www.wenhaofan.com/article/20180925232057 1.所有js统一在pjax容器外引入 在pjax容器外引入的js只会被引入一次,所以不会 ...
- rest_framework:响应器(渲染器)
一.作用: 根据用户的请求url或者用户可接受的类型.筛选出合适的渲染组件 用户请求url: http://127.0.0.1:8000/test/?format=json http://127.0. ...
- Princeton Landmarks in Mathematics and Physics
郎兰茲主页下载:http://10.12.0.10/sunsite.ubc.ca/DigitalMathArchive/Langlands/pdf/?mqcreq=1 http://10.12.0.1 ...
- Centos7部署jenkins
1. 下载rpm包: a) 下载地址:https://pkg.jenkins.io/redhat-stable/ b) 点选一个下载即可,例如点选:“jen ...
- SQL语句分类和语法
DQL:Data QueryLanguage 数据查询语言 作用: 查询表中的字段 命令: select 查询 ⑦select 查询列表①from 表1 别名②连接类型 joi ...
- python之路之css拾遗
做一个鼠标碰到就会自动加边框的效果 下边的代码,主要是使自动加边框的时候,加边框的部分不会跳动 实现一张图片的点击之后出现信息
- 图解SOAPUI解析WSDL文件
本文链接:https://blog.csdn.net/qq_16234613/article/details/53143279 新建项目 添加WSDL文件 查看方法 查看XML格式 运行测试
- 钉钉、钉应用(微应用和E应用)开发介绍
钉钉,数字化新工作方式,让工作更简单 目前在钉钉的官网可以看到,超过700万家企业组织正在使用钉钉.笔者也相信,这一数字每天都在增加.获得群众的认可,也是理所当然的,体验过钉钉,就能感觉到,钉钉的考勤 ...