Why do I need "sys.argv" to start a QApplication in PyQt?
Asked Answered
H

2

30

I try to understand what PyQt does. And one of the first things I didn't, was:

QApplication(sys.argv)

Why do I have to give QApplication this argument? I know what sys.argv does. But in my Scripts I wouldn't need it.

Hocker answered 14/1, 2015 at 10:10 Comment(3)
+ 1 for being curious and asking questions, also see #11713506Lamarlamarck
+1 for curiosity i like these kind of questions that asker wants to understand Why the code works instead of Why the code doesn't work it's really sad after 5 years of being on stackoverflow i see these questions don't get the love that they deserve (Saw some of them downvoted in some cases!!!) probably because most of coders only want the code to work it works job done. These quesions are more valuable than those What is the solution or Why there is an error in my code kind of quesions.Approximal
...oops... I've upvoted by a non-accident ;-)Sufficient
M
14

This calls the constructor of the C++ class QApplication. It uses sys.argv (argc and argv in C++) to initialize the QT application. There are a bunch of arguments that you can pass to QT, like styles, debugging stuff and so on.

Take a look at this for a full list of the options.

Monostich answered 14/1, 2015 at 10:21 Comment(3)
Is it not possible in C++ to give a default argument? So i have to insert always an empty list But i think, i will survive this.Hocker
@TobiasObermayer It is, but this is a convention in QT. Don't pass an empty list, pass the real sys.argv. At the very least it contains the name of the executable, which QT may or may not use.Monostich
If you start a GUI from an almost empty other script it fails at commandline. cus sys.argv are not the same I guess. At least the filenames are different and startup of app seems to be refused.Sufficient
M
7

QApplication takes a list of strings as input.

So you can forward sys.argv or simply an empty list:

app = QApplication([])
Marozik answered 14/1, 2015 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.