简体   繁体   English

QApplication应用程序(argc,argv)

[英]QApplication app(argc, argv)

I noticed that the main.cpp in a Qt application has to contain the following line: 我注意到Qt应用程序中的main.cpp必须包含以下行:

QApplication app(argc, argv);

I know that argc is the number of command-line arguments, and argv is that array list of command-line arguments. 我知道argc是命令行参数的数量,而argv是命令行参数的数组列表。 But, the question in my mind is: what are those arguments I'm passing to the constructor and at the same time cannot explicitly see? 但是,我的想法是: 我传递给构造函数的那些参数什么,同时又无法明确地看到? What is working behind the scences out there? 这些场景背后的工作是什么?

Thanks. 谢谢。

There are no hidden arguments. 没有隐藏的论点。 You can explicitly see every argument- argc, argv . 你可以明确地看到每个参数 - argc, argv There's nothing in that line of code that's behind the scenes. 幕后的那一行代码中没有任何内容。

Qt applications supports some command line arguments. Qt应用程序支持一些命令行参数。 For example app.exe -style motif on Windows is funny. 例如,Windows上的app.exe -style motif很有趣。 Basically, in this constructor, you pass the arguments to the QApplication class in order they to be parsed. 基本上,在此构造函数中,您将参数传递给QApplication类,以便对它们进行解析。

And of course you are free to pass nothing to QApplication, like this: 当然,您可以自由地将任何内容传递给QApplication,如下所示:

int c=1; char** v = &argv[0]; QApplication app(c,v); so Qt won't parse anything and won't accept any command line arguments. 所以Qt不会解析任何东西,也不会接受任何命令行参数。

About argc and argv scope, if you pass then to QApplication you can access them from every point you want. 关于argc和argv范围,如果您再传递给QApplication,则可以从所需的每个点访问它们。 If you don't pass them to QApplication, you have to take care yourself making them global. 如果你没有将它们传递给QApplication,你必须小心自己使它们成为全球性的。

From looking at your comments to other answers, I think you are wondering about arguments passed in to your executable if you don't specify any arguments. 从查看您的评论到其他答案,我认为您想知道如果您没有指定任何参数,传递给您的可执行文件的参数。 I'm not sure if it is standardized or what the exceptions may be, but usually in this case, argc will be 1 and argv[0] will be a string that specifies the command that was used to invoke your executable. 我不确定它是标准化的还是异常可能是什么,但通常在这种情况下, argc将为1argv[0]将是一个字符串,它指定用于调用可执行文件的命令。

Let's assume your executable is called app and resides in /home/user/appdir . 假设您的可执行文件名为app ,位于/home/user/appdir

If your current directory is the applications directory and you launch it with 'app' then argc will be 1 and argv[0] will be app . 如果您当前的目录是应用程序目录,并使用'app'启动它,则argc将为1argv[0]将为app

If you are one directory up from the application's directory and invoke it with ./appdir/app then argc will be 1 and I believe argv[0] will be appdir/app 如果您是应用程序目录中的一个目录并使用./appdir/app调用它,则argc将为1 ,我相信argv[0]将是appdir/app

If you do specify an argument when invoking your application; 如果在调用应用程序时指定了参数; perhaps you want to tell your application to output debug information like so app debug . 也许你想告诉你的应用程序输出调试信息,如app debug In this case, argc will be 2 , argv[0] will be app and argv[1] will be debug . 在这种情况下, argc将为2argv[0]将为appargv[1]将进行debug

If you are asking what QApplication does with arguments then the answer is in the docs . 如果您询问QApplication对参数的作用,那么答案就在文档中 That page lists the arguments recognised by Qt. 该页面列出了Qt识别的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM