PDA

View Full Version : installFilterEvent for all widgets



pakulo
13th May 2007, 22:41
How i can install filter events for all widgets that are situated in the main widget?

wysota
13th May 2007, 22:46
May I ask why do you want to do that?

pakulo
14th May 2007, 00:03
I want:
When i move mouse on some widget in the window it write coordinates of the cursor.

wysota
14th May 2007, 00:22
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.


class Widget : public QWidget {
public:
Widget() : QWidget(){
QHBoxLayout *l = new QHBoxLayout(this);
l->addWidget(new QPushButton);
l->addWidget(new QPushButton);
}
bool event(QEvent *e){
if(e->type()==QEvent::ToolTip){
QHelpEvent *he = static_cast<QHelpEvent*>(e);
QPoint p = he->globalPos();
QToolTip::showText(p, QString("%1x%2").arg(p.x()).arg(p.y()));
return true;
}
return QWidget::event(e);
}
};

pakulo
14th May 2007, 12:26
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

pakulo
14th May 2007, 12:31
Oh... sorry... it work... thank you, very good idea!
But i have some questions

pakulo
14th May 2007, 12:34
Why it is work only when i pressed the button?

wysota
14th May 2007, 12:54
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.

pakulo
14th May 2007, 13:19
If you prefer the event filter solution, you have to enable mouse tracking for all widgets.
How i can do it?

wysota
14th May 2007, 13:24
QWidget::setMouseTracking

pakulo
14th May 2007, 13:48
I know, but how i can set it for all widgets?
may be will be easy to set installEventFilter for all widgets?

wysota
14th May 2007, 14:38
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.

pakulo
14th May 2007, 16:34
Thank you... i decided to use

QList<QWidget*> widgets = qFindChildren<QWidget*>(this);
foreach(QWidget *widget, widgets)
widget->installEventFilter(this);

wysota
14th May 2007, 17:33
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.