Results 1 to 14 of 14

Thread: installFilterEvent for all widgets

  1. #1
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default installFilterEvent for all widgets

    How i can install filter events for all widgets that are situated in the main widget?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets

    May I ask why do you want to do that?

  3. #3
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    I want:
    When i move mouse on some widget in the window it write coordinates of the cursor.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets

    If you want to go for the eventFilter approach, you can use QLayout::itemAt() on the window's layout to query for an item on a specified position. Just check whether the item is a QWidgetItem and then you can ask for the widget it holds. You may also use qFindChildren<QWidget*> to get all the windows widgets, but if any of the widget contains other widgets, they'll be returned as well and this might not be what you want.

    But you can probably achieve a simmilar effect by reimplementing event() for your window and handling QEvent::ToolTip there. Provided that none of the child widgets have tooltips defined, you should receive this event for all of them in the top level window.

    Edit: I just verified the second approach, it works quite nice. Code below.

    Qt Code:
    1. class Widget : public QWidget {
    2. public:
    3. Widget() : QWidget(){
    4. QHBoxLayout *l = new QHBoxLayout(this);
    5. l->addWidget(new QPushButton);
    6. l->addWidget(new QPushButton);
    7. }
    8. bool event(QEvent *e){
    9. if(e->type()==QEvent::ToolTip){
    10. QHelpEvent *he = static_cast<QHelpEvent*>(e);
    11. QPoint p = he->globalPos();
    12. QToolTip::showText(p, QString("%1x%2").arg(p.x()).arg(p.y()));
    13. return true;
    14. }
    15. return QWidget::event(e);
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 13th May 2007 at 23:28.

  5. #5
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    I have just tested it, but dosn't work ((
    When i moving mouse on the parent widget it shows Tool Tip; But when move the child widget it doesn't show.
    Qt 4.2.2

  6. #6
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    Oh... sorry... it work... thank you, very good idea!
    But i have some questions

  7. #7
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    Why it is work only when i pressed the button?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets

    The tooltip solution should work out of the box for all children that don't have their own tooltips. If you prefer the event filter solution, you have to enable mouse tracking for all widgets.

  9. #9
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    If you prefer the event filter solution, you have to enable mouse tracking for all widgets.
    How i can do it?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets


  11. #11
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    I know, but how i can set it for all widgets?
    may be will be easy to set installEventFilter for all widgets?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets

    Quote Originally Posted by pakulo View Post
    I know, but how i can set it for all widgets?
    The same way you installed event filters to all widgets.
    may be will be easy to set installEventFilter for all widgets?
    I wouldn't advise that really I wouldnt' advise doing what you are doing at all. I'd probably reimplement QCoreApplication::event() and do some smart event manipulations there to achieve the effect you want in a simmilar manner to the tool tip system already present in the event loop.

  13. The following user says thank you to wysota for this useful post:

    pakulo (14th May 2007)

  14. #13
    Join Date
    May 2007
    Posts
    32
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: installFilterEvent for all widgets

    Thank you... i decided to use
    Qt Code:
    1. QList<QWidget*> widgets = qFindChildren<QWidget*>(this);
    2. foreach(QWidget *widget, widgets)
    3. widget->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: installFilterEvent for all widgets

    As you wish. Just be aware that some widgets' functionality (especially ones that are not implemented correctly) might get broken because of the use of mouse tracking.

  16. The following user says thank you to wysota for this useful post:

    pakulo (15th May 2007)

Similar Threads

  1. QLogText & QLogTable : 2 widgets to display text log
    By fcoiffie in forum Qt-based Software
    Replies: 7
    Last Post: 28th April 2019, 07:52
  2. New widgets after application begins
    By petty in forum Newbie
    Replies: 5
    Last Post: 8th May 2007, 12:10
  3. Performance in hiding/showing widgets
    By Paalrammer in forum Newbie
    Replies: 12
    Last Post: 14th February 2007, 18:57
  4. Replies: 11
    Last Post: 7th July 2006, 13:09
  5. Creating Widgets
    By hylke in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 08:37

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.