What do `argc` and `argv` mean in a Qt application?

0
2
Asked By MellowPine42 On

I'm learning C++ with Qt and noticed that QApplication is commonly created with `QApplication app(argc, argv);`. What do `int argc` and `char *argv[]` represent, and why does Qt need them?

3 Answers

Answered By BrightCedar7 On

They represent command-line arguments. `argc` is the number of arguments, while `argv` is an array of C-style strings containing those arguments. If you run `myprogram hello 123`, you’ll typically get `argc == 3`, with `argv[0]` set to the program name, `argv[1]` set to `"hello"`, and `argv[2]` set to `"123"`.

Answered By NimbusFox18 On

A typical C++ entry point looks like `int main(int argc, char* argv[])`. The `char* argv[]` part can be read as an array of pointers to characters, or more simply, an array of strings. There is also a null pointer at `argv[argc]` to mark the end of the array.

Answered By QuietMarble6 On

Qt accepts these values so it can inspect and process command-line options intended for Qt. For a basic application, you usually don’t need to interpret them yourself—just declare them in `main` and pass them to the constructor: `int main(int argc, char* argv[]) { QApplication app(argc, argv); return app.exec(); }`

MellowPine42 -

That makes sense. I had been using a `main()` with no parameters, so I can change it to accept `argc` and `argv` and pass them to QApplication.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.