Questions tagged [qapplication]

The QApplication class is part of the Qt C++ classes manages the GUI application's control flow and main settings.

The QApplication class manages the application's control flow and main settings. QApplication specializes QGuiApplication with some functionality needed for QWidget-based applications. It handles widget specific initialization, finalization.

For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.

Some GUI applications provide a special batch mode ie. provide command line arguments for executing tasks without manual intervention. In such non-GUI mode, it is often sufficient to instantiate a plain QCoreApplication to avoid unnecessarily initializing resources needed for a graphical user interface.

The QApplication object is accessible through the instance() function that returns a pointer equivalent to the global qApp pointer.

QApplication's main areas of responsibility are:

  • It initializes the application with the user's desktop settings such as palette(), font() and doubleClickInterval(). It keeps track of these properties in case the user changes the desktop globally, for example through some kind of control panel.
  • It performs event handling, meaning that it receives events from the underlying window system and dispatches them to the relevant widgets. By using sendEvent() and postEvent() you can send your own events to widgets.
  • It parses common command line arguments and sets its internal state accordingly. See the constructor documentation below for more details.
  • It defines the application's look and feel, which is encapsulated in a QStyle object. This can be changed at runtime with setStyle().
  • It specifies how the application is to allocate colors. See setColorSpec() for details.
  • It provides localization of strings that are visible to the user via translate().
  • It provides some magical objects like the desktop() and the clipboard().
  • It knows about the application's windows. You can ask which widget is at a certain position using widgetAt(), get a list of topLevelWidgets() and closeAllWindows(), etc.
  • It manages the application's mouse cursor handling, see setOverrideCursor().
160 questions
29
votes
2 answers

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

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.
Sir2B
  • 1,029
  • 1
  • 10
  • 17
18
votes
1 answer

qApp versus QApplication.instance()

With PyQt5, both of these return the application object: app = QtWidgets.QApplication.instance() app = QtWidgets.qApp for i in app.arguments()[1:]: ... But why does print(QtWidgets.QApplication.instance() is QtWidgets.qApp) print False?
Sir Visto
  • 683
  • 1
  • 4
  • 9
16
votes
3 answers

How to override QApplication::notify in Qt

I am trying to handle exception in my Qt application, I went through a couple of posts which indicated of overriding the QApplication::notify method to handle exceptions in a efficient way in Qt. I am not sure where should I add this overriden…
Valla
  • 2,414
  • 11
  • 42
  • 73
14
votes
1 answer

What is an event loop in Qt?

I have understood the following regarding QApplication's exec function: QApplication exec starts the main event loop. It launches the GUI. It processes the signals and calls appropriate slots on receiving them. It waits until exit is called and…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
13
votes
1 answer

How to get the current QApplication?

I am trying to get a reference to the current QApplication object with pyQt5, but couldn't find the function. My search about "pyQt get current QApplication" shows results about how to create an QApplication. So my question is: Is there a global…
thor
  • 21,418
  • 31
  • 87
  • 173
9
votes
1 answer

qt5 undefined reference to 'QApplication::QApplication(int&, char**, int)'

I'm trying to get a simple hello world example running, and already needed some time to figure out what includes to use Now I verified the include paths, the QApplication should actually be there, but it throws the above error. For clarity my…
Marcel H.
  • 197
  • 1
  • 2
  • 14
9
votes
1 answer

Is it possible to create local event loops without calling QApplication::exec()?

I'd like to create a library built on top of QTcpServer and QTcpSocket for use in programs that don't have event loops in their main functions (because the Qt event loop is blocking and doesn't provide enough timing resolution for the real-time…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
9
votes
2 answers

Segmentation fault when Qt QApplication created with new

I have a program where QApplication is created with the new operator. It crashes for unknown reason. I use RedHat Linux, G++ 4.8.2, Qt 4.7.2 which was built with the same compiler. This source contains many looking useless but harmless pieces, e.g…
8
votes
1 answer

Multiple QApplication instances

I'd like to know what are the implications (problems) of having multiple QApplication/QCoreApplication instances in the same process, and how to solve some issues regarding it. The scenario is as follows: I'd like to make a wrapper on a open source…
cbuchart
  • 10,847
  • 9
  • 53
  • 93
8
votes
1 answer

Qt: Getting the current application palette

I have a class that composes a palette and assigns it to the application using QApplication::instance()->setPalette(QPalette palette). And it effectively works. But then I try to use QPalette QApplication::instance()->palette() to extract some…
8
votes
4 answers

Qt qDebug not working with QConsoleApplication or QApplication

I currently have a terribly annoying problem while developing programs using Qt and Qt Creator. Whenever I try using qDebug() with a QCoreApplication or QApplication instantiated before using qDebug(), there isn't any output, whether I run the…
marius_linux
  • 595
  • 4
  • 14
7
votes
1 answer

Cleaning up after QApplication

I'm porting a Qt desktop application to Linux (Ubuntu 19.10, 64bit desktop, Qt 5.12.5, gcc version 9.2.1), and am seeing some unexpected threads remaining alive after QApplication is finished. Here's the minimum repro: #include…
DeducibleSteak
  • 1,398
  • 11
  • 23
6
votes
4 answers

Qt application with optional gui

I am going to write program using Qt for some image processing and I want it to be able to run in non-gui mode (daemon mode?). I'm inspired by VLC player, which is "typically" GUI program, where you can configure it using GUI, but you can also run…
d21d3q
  • 365
  • 2
  • 10
5
votes
2 answers

How does Qt select a default style?

In a Qt GUI application, QApplication::style()->objectName() will return the current style, for example "windowsvista". How/where does it choose this default style, and what information does it use to decide?
OJW
  • 4,514
  • 6
  • 40
  • 48
4
votes
1 answer

QOpenGLWidget with QApplication?

We have a QWidget based application that was previously using a QWindow for OpenGL rendering. To fit that window in our application we had to use QWidget QWidget::createWindowContainer(QWindow); Previously we only used external to Qt OpenGL…
ComradeJoecool
  • 734
  • 6
  • 18
1
2 3
10 11