Results 1 to 8 of 8

Thread: Mouse tracking outside the application interface

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Mouse tracking outside the application interface

    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:
    Qt Code:
    1. void MainWindow::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. qDebug() << "mouse move tracked!!";
    4. QPoint mousePoint = event->globalPos();
    5. int x = mousePoint.x();
    6. int y = mousePoint.y();
    7. qDebug() << x << ',' << y;
    8. QRect deskRect = screen.screenGeometry();
    9. int width = this->frameGeometry().width();
    10. int height = this->frameGeometry().height();
    11. if(x > (deskRect.width() - width) && y < 5)
    12. {
    13. this->move(deskRect.width() - width, 0);
    14. event->accept();
    15. // return;
    16. }
    17. if(x < (deskRect.width() - width) || y > height)
    18. {
    19. this->move(deskRect.width() - width, -height);
    20. event->accept();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    Quote Originally Posted by sophister View Post
    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 or maybe not... If the latter, you would have to install some sort of hook (again using platform specific code) to catch these events.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    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().
    from QCoreApplication::x11EventFilter() :
    You must install an event filter directly on the event dispatcher, which is returned by QAbstractEventDispatcher::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.
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    I have tried this QAbstractEventDispatcher::setEventFilter(EventFilter), but when I look for the EventFilter, I just get the following statement:
    Qt Code:
    1. typedef QAbstractEventDispatcher::EventFilter
    2.  
    3. Typedef for a function with the signature
    4.  
    5. bool myEventFilter(void *message);
    6. See also setEventFilter() and filterEvent().
    To copy to clipboard, switch view to plain text mode 
    So do you think what the exact param I have to pass into the function??

  6. #6
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    I have known that EventFilter is a functionwith a specific signature.
    But my question is, how to get the mouseMoveEvent through that function.

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    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 .

    Creating such an event filter is simple :
    Qt Code:
    1. bool myEventFilter(void *msg)
    2. {
    3. // even handling goes here
    4. }
    To copy to clipboard, switch view to plain text mode 

    then you set it as follows :
    Qt Code:
    1. QAbstractEventDispatcher::instance()->setEventFilter(myEventFilter);
    To copy to clipboard, switch view to plain text mode 

    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.
    Current Qt projects : QCodeEdit, RotiDeCode

  8. The following user says thank you to fullmetalcoder for this useful post:

    sophister (2nd May 2009)

  9. #8
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse tracking outside the application interface

    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.

Similar Threads

  1. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 13:06
  2. mouse tracking in QGraphicsItem
    By christina123y in forum Qt Programming
    Replies: 10
    Last Post: 9th March 2009, 08:23
  3. Replies: 11
    Last Post: 15th July 2008, 13:11
  4. Replies: 7
    Last Post: 8th September 2006, 16:19
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.