Results 1 to 8 of 8

Thread: Mouse event handling beyond/outside the widget

  1. #1
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Mouse event handling beyond/outside the widget

    Hi,

    Suppose I have a "Target" class derived from QWidget and I am creating it's object somewhere, suppose in "Source" class, in application. And I want to handle mouse events in this widget even if mouse event is not occured on this widget or occured outside of this widget.

    How can I achieve the above things?

    Thanks for your help.

    Thanks & Regards,
    Keyur Shah

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget


  3. The following user says thank you to caduel for this useful post:

    keyur259 (19th January 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget

    Thanks for your reply.

    I don't have much idea about installEventFilter().

    Can you post some code?

  5. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse event handling beyond/outside the widget

    event filter is an protected function to filter certain event occuring in QObject ex: mouse click, key press, mouse hover, double click
    ex:
    if i want to filter the key press of key F1
    Qt Code:
    1. bool DDView:: eventFilter(QObject *ob, QEvent *e)
    2. {
    3. if(e->type() == QEvent::KeyPress){
    4. printf("is it coming inside event..\n");
    5. const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    6. if(ke->key()==Qt::Key_F1){
    7. printf("F1 buttin clicked..\n");
    8. }
    9. return true;
    10. }
    11. return QWidget::eventFilter(ob, e);
    12. }
    To copy to clipboard, switch view to plain text mode 

    and in QWidget constructor add
    installEventFilter(this)
    "Behind every great fortune lies a crime" - Balzac

  6. The following user says thank you to wagmare for this useful post:

    keyur259 (20th January 2010)

  7. #5
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget

    Thanks for your reply. But I am still missing something. Do you know how to handle "QEvent::NonClientAreaMouseButtonPress"?

  8. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget

    Qt Code:
    1. event-filters are used like that
    2.  
    3. bool DDView:: eventFilter(QObject *ob, QEvent *e)
    4. {
    5. if (ob==someWidget & e->type() == QEvent::KeyPress) {
    6. printf("is it coming inside event..\n");
    7. const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    8. if(ke->key()==Qt::Key_F1) {
    9. printf("F1 buttin clicked..\n");
    10. }
    11. return true;
    12. }
    13. return QWidget::eventFilter(ob, e);
    14. }
    To copy to clipboard, switch view to plain text mode 
    where someWidget would be the widget whose events you want to intercept.
    You need to install the event-filter on that widget, first:
    Qt Code:
    1. someWidget->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    Note that QEvent::NonClientAreaMouseButtonPress are mousevents outside of any QWidget (iiuc) and can not be intercepted with an event filter.

    Are the mouse events you are interested in inside your window or just somewhere on the desktop? In the latter case you can use grabMouse().

  9. The following user says thank you to caduel for this useful post:

    keyur259 (20th January 2010)

  10. #7
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget

    I required to close widget if mouse is released outside the widget. There are few buttons inside the widget. I have called grabMouse() in showEvent() of the widget. But however I am able click outside the widget and widget looses it's focus.

    I have also tried popup widget, it works fine. But it hasn't titlebar & frame.

  11. #8
    Join Date
    Jan 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Mouse event handling beyond/outside the widget

    Do anyone have idea how to achieve it or it's not possible?

    Please do reply.

    Thanks & Regards,
    Keyur Shah

Similar Threads

  1. Replies: 7
    Last Post: 14th January 2010, 08:47
  2. Handling mouse over event for QListWidget
    By Abc in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 18:32
  3. pass mouse event information to another widget
    By Rooster in forum Qt Programming
    Replies: 5
    Last Post: 12th July 2008, 04:23
  4. event handling...
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 07:16
  5. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:44

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.