PDA

View Full Version : Can Qt4 capture mousePress events on the desktop?



simula
25th September 2007, 06:36
Hello,

In Qt4 is there a way to capture mouse events that occur when a user clicks inside another (non Qt) application? Ideally these events would be captured via Qt4 rather than something platform specific, would be full-blown QMouseEvents, and could be captured when the MainWindow is hidden.

I have played around a bit trying to monitor QDesktopWidget with an event filter, but I have nothing to show for it.

I appreciate any help anyone can throw my way,
Mark

wysota
25th September 2007, 11:47
You can enable mouse grabbing. This way you'll receive all mouse events that happen on the desktop. Don't forget to disable grabbing afterwards. The grabbing widget will have to be visible to be able to grab the mouse, but you can probably overcome that by creating a completely transparent widget (for example using setMask()).

simula
26th September 2007, 06:20
That was great help! Thanks a ton Wysota.

I am so close, but not quite there.

By using grabMouse() I am now able to grab all mouse events, even when the mouse is clicked outside of the QWidget. However, these events are now completely captured by my QWidget and do not pass into the third party application that the user clicks on the desktop.

I want the user to be able to click a third party application on the desktop (such as Firefox), for my QWidget to receive a corresponding QMouseEvent, but then I want the event to be passed onto the third party application. So that in effect I can store the x,y coordinates of the cursor when a person clicks the minimize button in a third party application without interfering with the user's attempt to minimize the third party application.

I can think of two possible ways to pass events back out to third party apps:

1) Qt bubbles the event up out of the QWidget, into Main Window, and out to the third party application. I have played with event->ignore(), but it only seems to help the event bubble out of the current QWidget and into Main Window, but not into a third party application.

2) If I could create a new OS mouse press event at the x, y coordinates that I picked up with the QMouseEvent from the original user mouse press. It seems that this might be possible with void QWSInputMethod::sendMouseEvent ( const QPoint & position, int state, int wheel ), but this is a QTopia only feature and I need to stick with Qt4.

Thanks for the help Wysota and I really appreciate any help completing this puzzle,
Mark

wysota
26th September 2007, 10:25
In general you have to perform this on OS level probably using native events (through QApplication::x11EventFilter or similar).

duduqq
31st December 2008, 06:15
Do you solve the problem,can you get me a TIP.

oob2
6th January 2009, 19:10
you should use native code;

if on win32, try this SetWindowsHookEx

if on X11, try


Window root = RootWindow(dpy,The_screen);
int buttons = 0;

/* Make the target cursor */
cursor = XCreateFontCursor(dpy, /*XC_crosshair*/XC_hand2);

/* Grab the pointer using target cursor, letting it room all over */
status = XGrabPointer(dpy, root, False,
ButtonPressMask|ButtonReleaseMask, GrabModeSync,
GrabModeAsync, root, cursor, CurrentTime);
if (status != GrabSuccess) {
return None;
}

/* allow one more event */
XAllowEvents(dpy, SyncPointer, CurrentTime);
XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
switch (event.type) {
case ButtonPress:
buttons++;
break;
case ButtonRelease:
if (buttons > 0) /* there may have been some down before we started */
buttons--;
break;
}

XUngrabPointer(dpy, CurrentTime); /* Done with pointer */