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 ...
随机推荐
- 微信小程序判断input是否为空
微信小程序中用到input值时候,判断其内容是否为空,可以用if-else判断内容的length,也可以给input加点击事件,判断其内容:以下是我解决问题的过程wxml代码 <view cla ...
- java单词统计
要求1:输出某个英文文本文件中26字母出现的频率,由高到低排序,并显示字母出现的百分比,精确到小数点后两位. 思路:分别设存放字母和字母出现次数的数组,遍历文件内容,将字母及出现频率按由高到低的顺序输 ...
- 最长公共子串(LCS) lg SP1811
后缀自动机的一大用处就是求最长公共子串了 这道题的话题意就是给你两个字符串,求最长公共子串 做法的话是先使用一个字符串建立SAM,然后让另一个串在上面进行匹配 匹配的策略是优先匹配当前节点的下一个字符 ...
- STL-priority_queue H - 看病要排队
H - 看病要排队 看病要排队这个是地球人都知道的常识.不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病情有轻重,所以不能 ...
- Codeforces Round #600 (Div. 2) A. Single Push
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; int T,n ...
- 《深入理解Java虚拟机》读书笔记一
第二章 Java内存区域与内存溢出异常 1.运行时数据区域 程序计数器: 当前线程所执行的字节码的行号指示器,用于存放下一条需要运行的指令. 运行速度最快位于处理器内部. 线程私有. 虚拟机栈: 描述 ...
- 10.3.4参数绑定 bind
Count_if算法,类似find_if,此函数接受一对迭代器,表示一个输入范围,还接受一个谓词,会对输入范围中的每个元素执行.Count_if返回一个计数值,表示谓词有多少次为真. 使用bin ...
- Django 初试水(二)
这部分链接上一部分.将建立数据库,创建第一个模型,并主要关注 Django 提供的自动生成的管理页面. 打开 mysite/setting.py 文件.这包含了 Django 项目设置的 Python ...
- soundtouch 变速算法matlab实现
soundtouch变速主要采用WSOLA算法来进行变速. http://www.surina.net/soundtouch/ https://blog.csdn.net/suhetao/articl ...
- WPF学习笔记三之绑定
1.绑定模式 <TextBlock Margin="10" Text="LearningHard" Name="lbtext" Fon ...