Question

Qt - top level widget with keyboard and mouse event transparency?

I want an app's main window to ignore mouse and keyboard events, passing them to applications underneath it in the window manager Z-order.

I see how to make child widgets ignore keyboard or mouse events, but how about the main window?

I'm trying to make a desktop widget that always sits just over the background and is totally invisible to keyboard and mouse events. (Pass through)

Qt::X11BypassWindowManagerHint gets me keyboard pass through (although sadly X11 specific, but fine for now), so how about mouse events?

Is there a OS-agnostic way to be transparent to keyboard events?

EDIT:

The key word here is transparency.

I don't want to EAT mouse and keyboard events, I want the window manager to know I don't want them at all. Those events should be directed to whatever application is under me in the zorder.

For example, I want to be able to click on desktop icons that are covered by my widget and interact with them as if the widget was not there.

 45  21231  45
1 Jan 1970

Solution

 13

I found the following solution (tested on Linux, also works on Windows according to @TheSHEEEP):

setWindowFlags(windowFlags() | Qt::WindowTransparentForInput);

It has been added in more recent qt release (i did not find when) see http://doc.qt.io/qt-5/qt.html

2016-08-03

Solution

 11

On Windows you can set WS_EX_TRANSPARENT

To do this in Qt use the following code:

Include the header,

#if _WIN32
    #include <windows.h>
#endif

and put the following code into the constructor.

#if _WIN32
    HWND hwnd = (HWND) winId();
    LONG styles = GetWindowLong(hwnd, GWL_EXSTYLE);
    SetWindowLong(hwnd, GWL_EXSTYLE, styles | WS_EX_TRANSPARENT);
#endif
2011-02-08