QApplication 应用程序(argc, argv)
我注意到Qt
应用程序中的main.cpp
必须包含以下行:
QApplication app(argc, argv);
我知道argc
是命令行的数量参数,argv 是命令行参数的数组列表。但是,我心中的问题是:我传递给构造函数但同时无法明确看到的参数是什么?幕后到底是什么在起作用?
谢谢。
I noticed that the main.cpp
in a Qt
application has to contain the following line:
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有隐藏的论点。您可以显式地查看每个参数 - argc、argv。这行代码中没有任何幕后的内容。
There are no hidden arguments. You can explicitly see every argument-
argc, argv
. There's nothing in that line of code that's behind the scenes.从查看您的评论到其他答案,我认为您想知道如果您没有指定任何参数,则传递给可执行文件的参数。我不确定它是否标准化或者可能有什么例外,但通常在这种情况下,
argc
将是1
和argv[0] 将是一个字符串,指定用于调用可执行文件的命令。
假设您的可执行文件名为
app
并驻留在/home/user/appdir
中。如果您当前的目录是应用程序目录并且您使用“app”启动它,则
argc
将是1
并且argv[0]
将是 <代码>应用程序。如果您位于应用程序目录的上一级目录并使用
./appdir/app
调用它,那么argc
将是1
并且我相信argv[0]
将是appdir/app
如果您在调用应用程序时指定了参数;也许您想告诉您的应用程序输出调试信息,例如
app debug
。在这种情况下,argc
将是2
,argv[0]
将是app
和argv[ 1]
将进行调试
。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 be1
andargv[0]
will be a string that specifies the command that was used to invoke your executable.Let's assume your executable is called
app
and resides in/home/user/appdir
.If your current directory is the applications directory and you launch it with 'app' then
argc
will be1
andargv[0]
will beapp
.If you are one directory up from the application's directory and invoke it with
./appdir/app
thenargc
will be1
and I believeargv[0]
will beappdir/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
. In this case,argc
will be2
,argv[0]
will beapp
andargv[1]
will bedebug
.Qt 应用程序支持一些命令行参数。例如,Windows 上的
app.exe 样式主题
很有趣。基本上,在此构造函数中,您将参数传递给 QApplication 类以便解析它们。当然,您可以不向 QApplication 传递任何内容,如下所示:
int c=1; char** v = &argv[0]; QApplication app(c,v);
因此 Qt 不会解析任何内容,也不会接受任何命令行参数。关于 argc 和 argv 范围,如果您将 then 传递给 QApplication,您可以从您想要的每个点访问它们。如果您不将它们传递给 QApplication,则必须小心地将它们设置为全局的。
Qt applications supports some command line arguments. For example
app.exe -style motif
on Windows is funny. Basically, in this constructor, you pass the arguments to the QApplication class in order they to be parsed.And of course you are free to pass nothing to QApplication, like this:
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.About argc and argv scope, if you pass then to QApplication you can access them from every point you want. If you don't pass them to QApplication, you have to take care yourself making them global.
如果您问
QApplication
对参数执行什么操作,那么答案是 在文档中。该页面列出了 Qt 识别的参数。If you are asking what
QApplication
does with arguments then the answer is in the docs. That page lists the arguments recognised by Qt.