Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/987019/qt-top-level-widget-with-keyboard-and-mouse-event-transparency

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...