QApplication app(argc, argv)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 12:46:39

问题


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.


回答1:


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.




回答2:


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.

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 be 1 and argv[0] will be 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

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 be 2, argv[0] will be app and argv[1] will be debug.




回答3:


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.




回答4:


If you are asking what QApplication does with arguments then the answer is in the docs. That page lists the arguments recognised by Qt.



来源:https://stackoverflow.com/questions/5770154/qapplication-appargc-argv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!