I started learning PyQt from zetcode, as far as i know sys.argv reads in the parameter from coomandline and first being the name of file itself, so if sys.argv is a list then what being passed to QtGui.QApplication() in

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Dani AI

Generated

is spot on: you pass sys.argv so Qt can see and act on command‑line options that it understands. Under the hood Qt parses the list and removes any options it recognizes (for example -style, -stylesheet, -widgetcount, -reverse), leaving the rest for your app to handle. This is documented in Qt’s QApplication constructor: Qt may change argc/argv as it consumes its own flags. See the command‑line options list and that note in the Qt docs. QApplication docs.

Building on ’s demo: passing [] works, but it also disables those Qt CLI features. If you want both Qt’s flags and your own, either parse after creating the app (Qt will have stripped its flags already), or pre‑parse your flags and forward only the leftovers to Qt. Here is a pattern many PyQt4/PyQt5 apps use:

# parse your app's flags first, let Qt handle the rest
import sys, argparse
from PyQt4 import QtGui  # use PyQt5/PyQt6 equivalents as needed

parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--input")
app_args, qt_args = parser.parse_known_args(sys.argv[1:])

app = QtGui.QApplication([sys.argv[0]] + qt_args)
# now use app_args.input, and create your UI...

Tips you can try right away:

  • Launch with python yourapp.py -style fusion (or another available style) to see Qt pick it up; availability depends on installed styles/plugins. QApplication docs.
  • If you prefer a Qt‑native way to parse your own options (Qt 5+), use QCommandLineParser alongside QCoreApplication::arguments(). It is designed to coexist with Qt’s built‑ins. QCommandLineParser docs.

So, sys.argv is not just the filename; it is how you let Qt configure itself from the CLI while still keeping your own flags cleanly separated.

Recommended Answers

All 8 Replies

Because this call invokes an underlying C++ function which uses argv and doesn't know about python. Qt understand a set of command line arguments, see for example.

ok then what is in the sys.argv list ?

Here is an example program

#!/usr/bin/env python
# testargv.py

if __name__ == "__main__":
    import sys
    print(sys.argv)

"""my command line -->

testargv.py -foo bar --baz qux -o foobar.txt bazqux.pdf

my output -->

['./testargv.py', '-foo', 'bar', '--baz', 'qux', '-o', 'foobar.txt', 'bazqux.pdf']
"""

sys.argv is the list of command line arguments that were given to the program.

well i do understand that what would it be incase of app = QtGui.QApplication(sys.argv)

Try -style motif for example, or any of the arguments listed

Let's give it a try:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

# ----- start your widget test code ----


# create the label
label = QLabel()
label.show()
# insert a text string
label.setText('Hello fron DaniWeb!')
print(sys.argv)


# ---- end of widget test code -----

app.exec_()

''' my console output -->
['C:\\Python33\\BS\\pqt_sys_argv.py']
'''

QApplication() expects a list, but you can feed it an empty list if you don't want to putz around with sys.argv

from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication([])

# ----- start your widget test code ----


# create the label
label = QLabel()
label.show()
# insert a text string
label.setText('Hello fron DaniWeb!')


# ---- end of widget test code -----

app.exec_()
commented: good help +13

nice thanks, i am learning now, i think it would good if i buy book though.. however now another question strikes, i cant avoid asking back so here it goes, if QApplication() expects a list, and we can pass in [] empty list then why sys.argv is used in every tutorial of zetcode, like Gribouillis said why not pass the -style "motif"... ?

why not pass the -style "motif"... ?

I tried sneekula's code with -widgetcount and it worked as described in the doc. The best thing to do is to run your code with various arguments and see what it does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.