PDA

View Full Version : eventFilter: test if enter a subWidget in my subclassed QWidget



vonCZ
9th December 2007, 12:40
I've subclassed QWidget to make a button panel: BuPan. BuPan consists of buttons, a slider, and checkBoxes: it's a small panel that pops up next to a QPushButton in my mainApplication like so:


bool Window:eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::Enter)
{
if(obj == myQPushButton)
{
myBuPan->move(xloc, yloc);
myBuPan->show();
return QWidget::eventFilter(obj, event)
}
etc...
}


pb[0] is a button in myBuPan. I want to check for an Enter event on this particular button within BuPan; I thought I could do the following:


bool Window:eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::Enter)
{
if(obj == myQPushButton)
{
myBuPan->move(xloc, yloc);
myBuPan->show();
return QWidget::eventFilter(obj, event);
}
if(obj == myBuPan->get_pb(0))
{
qDebug("HIT!!");
return QWidget::eventFilter(obj, event);
}
etc...

// ...and I have the following in bupan.h:

QList<QPushButton *> pb;
QPushButton *get_pb(int value) { return pb[value]; }


Shouldn't this work? ...er, it's not.:o ...ideas?

Or will I have to add an "enterEvent(QEvent *event)" method to the BuPan class itself? If I have to do this second option: How can I write the bupan "enterEvent" method so that it's generic... for example, so that pb[0] and pb[2] will capture enterEvents on one instance of BuPan, but on another instance only pb[4] might capture the events?

marcel
9th December 2007, 12:54
You can either install the window as event filter for BuPan and the button inside BuPan, and leave your code like it is now, or(better), install BuPan as event filter for the button, and create an eventFilter for BuPan.

vonCZ
9th December 2007, 15:52
You can either install the window as event filter for BuPan and the button inside BuPan, and leave your code like it is now,

thanks Marcel. The reason it wasn't working: I didn't installEventFilter() properly. Working now.


or(better), install BuPan as event filter for the button, and create an eventFilter for BuPan.

I'm still not sure how to do it this way. Currently--in my mainApplication eventFilter ("Window")--I include events to filter in by referencing specific widget-objects to test whether the mouse enters/leaves the objects. But I don't know how to create an eventFilter for a subclassed QWidget that would be flexible. For example, some of my BuPan instances will have 10+ buttons, none of which need to be filtered, whereas other BuPans might have 7 buttons, half of which need to be filtered, etc...

wysota
9th December 2007, 17:41
I'd suggest intercepting QEvent::ToolTip instead of QEvent::Enter...

Gopala Krishna
9th December 2007, 19:08
I'd suggest intercepting QEvent::ToolTip instead of QEvent::Enter...

Just for curiosity and my better understanding. Can you explain the reason for this ?

marcel
9th December 2007, 19:15
Just for curiosity and my better understanding. Can you explain the reason for this ?
So that you can eliminate "accidental" hovering.

Gopala Krishna
10th December 2007, 05:06
So that you can eliminate "accidental" hovering.

Sorry for posting out of topic but one final question. Is this accidental hovering prevented by using timers , while sending Tooltip event ?

marcel
10th December 2007, 06:59
Yes, most likely. On enter a timer is started and on leave the timer is disabled.
When and if the timer triggers, the tooltip event is posted.