14

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 returns the value which was set in exit.

Now, when we say event loop, does it mean that there is some while loop running in the internal code of Qt, and in that while loop the method of handling signals and slots is written?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 2
    Event loop means that your code is continuously running, think about it as being refreshed every time, so changes will be seen and made continuously based off your cases you have. – Omid CompSCI Nov 30 '16 at 05:33
  • You can think of it as an event driven program, just continuously waiting for events and will do something. Event loops are used in most GUI's and Games. In game development there is an event loop, because you need to check continuously if the user does something, there needs to be an event that occurs. – Omid CompSCI Nov 30 '16 at 06:13
  • And Signals and Slots are basically your "If this happens" do "this". – Omid CompSCI Nov 30 '16 at 06:13
  • 3
    Directly connected slots don't go to the event queue, only queued connections. – thuga Nov 30 '16 at 07:23
  • 1
    This was really helpful to me, have a look [link](https://wiki.qt.io/Threads_Events_QObjects) – Andre Nov 30 '16 at 14:18
  • @Andre thanks much. – Aquarius_Girl Nov 30 '16 at 17:49
  • 4
    Note that the event loop has nothing to do with the concept of signals and slots per se. It is used as an implementation detail in delivering slot/functor calls in queued connections or across threads in automatic connections. – Kuba hasn't forgotten Monica Nov 30 '16 at 20:33
  • @KubaOber I don't know about queued connections and automatic connections. Please write a detailed answer . Thanks. – Aquarius_Girl Dec 01 '16 at 01:04
  • 1
    In that case, you should temporarily forget that signals and slots have anything to do with event loops. First and foremost, the signal-slot mechanism is a nice wrapper around calls via a method pointer. With added multithreading superpowers that happen to depend on the event loop. But it's rather misleading to think of signal-slot system as somehow implemented primarily using event loops. Signals and slots work just fine without presence of a single event loop anywhere in the application. – Kuba hasn't forgotten Monica Dec 01 '16 at 04:28
  • To add to @UnslanderMonica's last message. Boost's signals and slots for example don't have an event loop. As far as I know, the event loop lets you execute slots on the receiving thread as opposed to the thread that calls (emits) the signal. – pooya13 Sep 27 '20 at 22:37

1 Answers1

10

Now, when we say event loop, does it mean that there is some while loop running in the internal code of Qt, and in that while loop the method of handling signals and slots is written?

In a sense, yes. Most software these days sits and waits for events -- user input, network traffic, timer events, sensors, etc. -- and responds accordingly.

This is not specific to Qt. It's a common design pattern you'll find everywhere from Windows to Android to Arduino.

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
  • 23
    It's not only "in a sense", it's quite literally true. The while loop can be found at line 203 of $QTDIR/src/corelib/kernel/qeventloop.cpp, inside QEventLoop::exec(): while (!d->exit.loadAcquire()) processEvents(flags | WaitForMoreEvents | EventLoopExec); – Jeremy Friesner Nov 30 '16 at 07:00