PDA

View Full Version : Mouse tracking outside the application interface



sophister
1st May 2009, 17:28
Hi, I try the following codes to track the mouse outside the application, and when the mouse enters a specific rect (just enter, no click), show the application mainwindow; and move the mainwindow out of the screen when the mouse leave tha specific area:

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "mouse move tracked!!";
QPoint mousePoint = event->globalPos();
int x = mousePoint.x();
int y = mousePoint.y();
qDebug() << x << ',' << y;
QRect deskRect = screen.screenGeometry();
int width = this->frameGeometry().width();
int height = this->frameGeometry().height();
if(x > (deskRect.width() - width) && y < 5)
{
this->move(deskRect.width() - width, 0);
event->accept();
// return;
}
if(x < (deskRect.width() - width) || y > height)
{
this->move(deskRect.width() - width, -height);
event->accept();
}
}

But the problem is, when I move the mouse outside the mainwindow, there is no MouseMoveEvent, and this function is never called.
why??

fullmetalcoder
1st May 2009, 17:50
But the problem is, when I move the mouse outside the mainwindow, there is no MouseMoveEvent, and this function is never called.
why??
Because the windows system does not send events to windows that are not concerned so Qt does not generate the related QEvents (and it wouldn't know to which widget send them if it did). AFAIK the only way to do mouse tracking outside a window is to mess with platform specific events.
It might be possible to catch all mouse events through QApplication platform specific handlers or QAbstractEventDispatcher (http://doc.trolltech.com/latest/qabstracteventdispatcher.html) or maybe not... If the latter, you would have to install some sort of hook (again using platform specific code) to catch these events.

sophister
1st May 2009, 18:05
Thank you!
Uh, do you have a sample code to show how to use the QAbstractEventDispatcher??
Or where should I use it , in the main.cpp or in the mainwindow class??
Thanks!

fullmetalcoder
1st May 2009, 18:25
I have never used QAbstractEventDispatcher myself but the docs suggest that it could help you :

from QCoreApplication::winEventfilter() :

To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher::instance (http://doc.trolltech.com/4.3/qabstracteventdispatcher.html#instance)().

from QCoreApplication::x11EventFilter() :

You must install an event filter directly on the event dispatcher, which is returned by QAbstractEventDispatcher::instance (http://doc.trolltech.com/4.3/qabstracteventdispatcher.html#instance)(), to handle system wide messages.

So it turns out that messing with platform specific events may not be required and that bypassing the filtering stage may be enough to catch mouse moves outside windows. If this does not work, setting an event filter through QAbstractEventDispatcher::setEventFilter could do but would require platform-specific code.

As for where to use it I'd say installing an event filter makes more sense in your mainwindow that in an extra class in you main but it's up to you.

You try and tell us if it works.

sophister
1st May 2009, 19:25
I have tried this QAbstractEventDispatcher::setEventFilter(EventFilt er), but when I look for the EventFilter, I just get the following statement:

typedef QAbstractEventDispatcher::EventFilter

Typedef for a function with the signature

bool myEventFilter(void *message);
See also setEventFilter() and filterEvent().
So do you think what the exact param I have to pass into the function??

sophister
1st May 2009, 19:30
I have known that EventFilter is a functionwith a specific signature.
But my question is, how to get the mouseMoveEvent through that function.

fullmetalcoder
1st May 2009, 20:11
Hmm looks like I made a slight confusion. QAbstractEventDispatcher is a QObject so I thought it would be possible to intercept event using QObject::setEventFilter but that probably isn't possible and the event filter refered to in the to quotes above is more likely the one defined in QAbstractEventDispatcher :o.

Creating such an event filter is simple :


bool myEventFilter(void *msg)
{
// even handling goes here
}


then you set it as follows :

QAbstractEventDispatcher::instance()->setEventFilter(myEventFilter);

Then it gets real complicated because you got to do the actual event handling in myEventfilter and that is platfrom specific. Unfortunately I cannot help you about that since I have very little knowledge of the topic. Still you should be able to do something after investigating Qt sources to figure out how to handle the messages sent to that filter but that won't be pretty.

sophister
2nd May 2009, 06:44
Thanks a lot!! You have helped much.
I get some information from text book. It says that to install EventFilter on the qApp (i.e, QApplication) instance is a way to send message to the windows that have lost focus.