QApplication object is the core of every Qt Widgets application. Every application needs one and only one
QApplication object to function. This object starts and holds the main event loop of your application which governs all user interaction with the GUI.
When developing applications with PyQt or PySide you will have created an instance of
QApplication like in the example below, passing in
sys.argv or an empty list
import sys from PySide6.QtWidgets import QApplication app = QApplication(sys.argv) # or: app = QApplication()
import sys from PyQt6.QtWidgets import QApplication app = QApplication(sys.argv) # or: app = QApplication()
While this works, you may have asked yourself: what is the meaning of
sys.argv or the empty list
 and why do you need to pass it to
When your application starts,
sys.argv contains the arguments used to launch your application. If you start a Python application from the command line,
sys.argv will contain the name of your Python script file as the first entry.
argv is an abbreviation of argument vector. This name originates from the C programming language, where “vector” is the name used for a dynamic array or
For example, see the example command line below:
When executing this script
sys.argv will contain the list
['argtest.py']. Anything further you add to the end of the command above will be appended to
To see this in action create the following simple Python script and save it under the name
import sys print("Arguments:", sys.argv)
Now, run your
argtest.py script from the command line and experiment with passing in your own arguments. For example, the following command line:
python argtest.py --info
Arguments: ['argtest.py', '--info']
As you can see, the
--info command-line argument is retrieved from
sys.argvbeside the filename of the Python script.
Command-line arguments are split on spaces, so anything separated with a space will be treated as a new argument. To pass arguments that include spaces, you need to wrap those in quotes.
When we pass
QApplication we are passing the command-line arguments to Qt. This allows us to pass any configuration settings to Qt at the startup of an application.
What command-line arguments are available?
Qt provides some built-in command-line arguments which are also available in PyQt and PySide.
Qt-specific command-line arguments
Specifies the Qt Platform Abstraction (QPA) plugin. This overrides the QT_QPA_PLATFORM environment variable.
This is commonly used when developing applications on the Raspberry Pi to redirect Qt output to a linux framebuffer device (eg a custom screen) without using XWindows. For example,
Specifies the path to platform plugins. This overrides the QT_QPA_PLATFORM_PLUGIN_PATH environment variable.
Specifies the platform theme. This overrides the QT_QPA_PLATFORMTHEME environment variable.
Specifies additional plugins to load. The plugin value may have passed multiple times. This is concatenated with the plugins in the QT_QPA_GENERIC_PLUGINS environment variable.
port:1234[,block]where block is optional and will make the application wait until a debugger connects to it.
Specifies a geometry for the main window using the X11 syntax which should be passed like
Sets the default icon of a window.
Sets a title of the first window.
Sets the application’s layout direction to
Qt.RightToLeft. This option is intended to aid debugging and should not be used in production. The default value is automatically detected from the system’s locale.
Restores the application from an earlier session.
General command-line arguments
Displays help on all the general command-line arguments, including this one. Note that the
-?argument is only available on Windows.
--helpdoes plus all the Qt-specific command-line arguments.
Displays the version of an application.
Downloadable ebook (PDF, ePub) & Complete Source code
To support developers in [[ countryRegion ]] I give a
[[ localizedDiscount[couponCode] ]]% discount
with the code [[ couponCode ]]— Enjoy!
For [[ activeDiscount.description ]] I’m giving a
[[ activeDiscount.discount ]]% discount
with the code [[ couponCode ]]— Enjoy!
Do I need to pass
No. While passing
sys.argv allows you to pass command-line configuration through to Qt at the startup of your application, if you don’t need or want to prevent configuration of Qt from the command line, you can instead either pass an empty list
 (using PyQt6, PyQt5, PySide2) or nothing at all (using PySide6).
# PyQt6 import sys from PyQt6.QtWidgets import QApplication app = QApplication()
# PySide6 import sys from PySide6.QtWidgets import QApplication app = QApplication()
If you do not pass
sys.argvyou are not forwarding command-line arguments to Qt. Therefore, you can not customize Qt‘s behavior from the command line.
QApplication at startup allows you to customize the behavior of
Qt from the command-line. If you don’t want to pass command-line arguments to Qt you can skip passing
sys.argv and instead pass nothing (PySide6) or
 (other PyQt or PySide versions).
Be sure to double check if you need any of Qt’s command-line configuration — for example, you’re developing for Raspberry Pi — before disabling this behavior.