#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 解析命令行参数的更多相关文章

  1. Qt之命令行参数

    简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设 ...

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

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  3. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  4. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  5. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  6. C语言中使用库函数解析命令行参数

    在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...

  7. Windows下解析命令行参数

    linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...

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

     1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...

  9. QT解析命令行(QCommandLineOption和QCommandLineParser类)

    Qt从5.2版开始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行参数. 一.命令行写法命令行:"-abc" 在QComma ...

随机推荐

  1. springboot 扫描不到包 @SpringBootApplication 自动配置原理

    解决方案 在main类中增加注解 @ComponentScan("com.test.test.*") 扫描具体的包 @ComponentScan(basePackages = {& ...

  2. 左边div固定宽度,右边div自适应撑满剩下的宽度--实现方法汇总

    神奇的事 其实有的方法(float.position.margin.flex)是有border像素的差 代码如下: <!DOCTYPE html><html><head ...

  3. RabbitMQ使用注意事项

    用ConnectionFactory创建的TCP连接要复用,因为创建新的TCP连接比较耗时. IModel(信道)是轻量级的,可以用时创建. channel.BasicQos(0, 1, false) ...

  4. [MongoDB] 使用PHP在MongoDB中搜索的实现

    条件操作符用于比较两个表达式并从mongoDB集合中获取数据.MongoDB中条件操作符有:(>) 大于 - $gt(<) 小于 - $lt(>=) 大于等于 - $gte(< ...

  5. Vue 嵌套路由使用总结

    Vue 嵌套路由使用总结   by:授客 QQ:1033553122   开发环境   Win 10   node-v10.15.3-x64.msi 下载地址: https://nodejs.org/ ...

  6. Java 并发核心机制

    目录   一.J.U.C 简介  二.synchronized  三.volatile  四.CAS  五.ThreadLocal  参考资料

  7. 更新centos本地仓库(换源)

    /etc/yum.repos.d/CentOS-Base.repo 1,首先进行备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  8. 查询linux版本信息

    uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) cat /etc/redhat-release (Linux查 ...

  9. ping和tracert

    ping命令常用于测试2台主机网络是否连通 TTL的默认值有:64(linux),128(windows),255(路由器) 此例TTL是63所以选用64来减去63等于1,这是说明经过了1个路由器,没 ...

  10. Cadence Allegro小技巧之指定Gerber生成路径

    Allegro生成Gerber数据时,默认会保存在与pcb文件相同目录路径下,Gerber数据本身就会生成好几个文件,然后与pcb文件,log文件,临时文件等混杂在一起,不易整理打包Gerber数据, ...