29

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.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Sir2B
  • 1,029
  • 1
  • 10
  • 17
  • 7
    + 1 for being curious and asking questions, also see http://stackoverflow.com/questions/11713006/elegant-command-line-argument-parsing-for-pyqt – NoDataDumpNoContribution Jan 27 '15 at 22:40
  • 9
    +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. – pouya Aug 25 '17 at 19:28
  • 1
    ...oops... I've upvoted by a non-accident ;-) – ZF007 Mar 17 '18 at 13:30

2 Answers2

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.

dmg
  • 7,438
  • 2
  • 24
  • 33
  • 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. – Sir2B Jan 14 '15 at 12:28
  • 1
    @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. – dmg Jan 14 '15 at 13:03
  • 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. – ZF007 Mar 17 '18 at 13:33
7

QApplication takes a list of strings as input.

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

app = QApplication([])
sebastian
  • 9,526
  • 26
  • 54