开发者

QApplication app(argc, argv)

开发者 https://www.devze.com 2023-02-28 22:01 出处:网络
I noticed that the main.cpp in a Qt application has to contain the following line: QApplication app(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 que开发者_运维技巧stion 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. There's nothing in that line of code that's behind the scenes.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号