Fady Megally
Fady Megally

Reputation: 145

How is QApplication notified by the window system?

I started learning Qt with C++ and I trying to figure out how the event mechanism works. I kind of understand how it works within the application itself, and how QApplication handles the events and queue them for processing and notifying objects/widgets about them.

I still have some open questions though:

  1. How's the QApplication itself notified by the windowing system about events?
  2. How does the windowing system know about the internal naming of my widgets, so that it can tell QApplication that widget "x" was clicked, focused, etc.
  3. If the window system doesn't know the widget names or doesn't have a reference to the clicked widget (or any other kind of event), how does QApplication figure out which widget needs to be notified.

Thanks in advance for your help.

Upvotes: 1

Views: 721

Answers (2)

ixSci
ixSci

Reputation: 13698

In windows, for example, QWidget is just a plain window with hwnd and all other related stuff. You can even get the hwnd by calling effectiveWinId() method. And it uses standard Windows Messages Loop to get notified about all Windows events, which you can get your hands on by installing an appropriate filter via QCoreApplication::setEventFilter. But Qt has also QML part and other non-native stuff and for that to work it uses some internal bookkeeping which depends on the nature of "alien widget"

All the aforementioned is true for non-alien widgets and alien widgets are somewhat different. Alien widgets are managed by the Qt itself, and it is a Qt job to identify for what QWidget action should be propagated and what the action is. Qt uses alien widgets by default since 4.4 but they could be easily inalienated if needed.

Other OSs differ in implementation but idea is the same.

Upvotes: 1

Nikos C.
Nikos C.

Reputation: 51840

On X11, Qt polls xlib (Qt4) or XCB (Qt5) for events. With xlib, that would be the XNextEvent() function and other related ones:

http://www.x.org/archive/X11R7.5/doc/man/man3/XMaskEvent.3.html

If you grep though the Qt source code for "XNextEvent", you can find the places where it's used. For example, in the Qt 4.8.4 sources:

$ grep -r XNextEvent
tools/qvfb/x11keyfaker.cpp:         XNextEvent(dpy, &event);
src/plugins/platforms/xlib/qxlibscreen.cpp:            XNextEvent(mDisplay->nativeDisplay(), &event);
src/gui/kernel/qwidget_x11.cpp:            XNextEvent(X11->display, &ev);
src/gui/kernel/qguieventdispatcher_glib.cpp:            XNextEvent(X11->display, &event);
src/gui/kernel/qeventdispatcher_x11.cpp:                XNextEvent(X11->display, &event);
src/gui/kernel/qapplication_x11.cpp:            XNextEvent(X11->display, &nextEvent);

With XCB, there's xcb_wait_for_event().

Once Qt has the event, it can start dispatching it through the widget hierarchy and to QML.

This is only for X11 of course (meaning Unix and Linux.) Other platforms have different ways of delivering events to applications.

Upvotes: 2

Related Questions