PDA

View Full Version : Assigning event filter for widgets inside groupbox



mania
19th June 2014, 12:05
Hi All,
I have used three push buttons inside a groupbox and when I assign eventFilter() for the pushbuttons it gets hidden in the output screen. groupbox has NoFocus policy and pushbuttons has StrongFocus policy.


test1::test1(QWidget *parent) :
QDialog(parent, Qt::FramelessWindowHint),
ui(new Ui::test1)
{
ui->setupUi(this);
ui->pushButton_1->installEventFilter(this);
ui->pushButton_2->installEventFilter(this);
ui->pushButton_3->installEventFilter(this);
}


bool test1::eventFilter(QObject *o, QEvent *e)
{
if(e->type()==QEvent::KeyPress)
{
return QObject::eventFilter(o,e);
}
}

stampede
19th June 2014, 15:01
Pay atention to compiler warnings, it should warn you that there is no return value in function returning bool:


bool test1::eventFilter(QObject *o, QEvent *e){
if(e->type()==QEvent::KeyPress){
return QObject::eventFilter(o,e);
}
// and if not keyPress then ... ?
}

Result returned by such function is undefined. Probably you are filtering out the paint events for these widgets.
To fix this, always return default base class eventFilter() when you don't want to filter the event out:


bool test1::eventFilter(QObject *o, QEvent *e){
if(e->type()==QEvent::KeyPress){
// handle key press
return true; // filter out this event
}
return QDialog::eventFilter(o,e); // or whatever is the base class for test1
}

To avoid such errors in the future, add this flag to compiler flags:


-Werror=return-type

or better


-Wall -Werror

anda_skoa
19th June 2014, 15:04
Your event filter is not returning for all cases, so the show or paint events are likely filtered out before they reach the buttons.

What is it that you want to achieve?

Cheers,
_

d_stranz
21st June 2014, 23:37
Pay attention to compiler warnings, it should warn you that there is no return value in function returning bool

Interesting. Visual C++ will not let you get away with this - it's a compilation error, not a warning. All paths must return a valid value when the return type is not void.