PDA

View Full Version : C++/Qt5 - Install event filter on UI QPushButton



jimbo
22nd December 2015, 13:01
Qt Creator 3.5.1 (opensource)
Based on Qt 5.5.1 (MSVC 2013, 32 bit)
Windows 7

Hello,

This is the first time I've used the form designer in QtCreator.
So far, so good. I have form that displays what I want.
The problem comes when I try and install an event filter on a QPushButton (bRefresh).
The button does not display, If I place the cursor where the pushbutton should be the eventFilter works.
If I do not install the event filter the button shows, but of course the eventFilter does'nt get called.
What am I doing wrong?

Regards
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
...
...
bRefresh->installEventFilter(this); //is a QPushButton
}

#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent=0);
~MainWindow();
bool eventFilter(QObject *obj, QEvent *event);

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == (QObject*)bRefresh)
{
if (event->type() == QEvent::Enter)
{
QApplication::setOverrideCursor(QCursor(Qt::ArrowC ursor));
qDebug() << "enter";
} else {
if (event->type() == QEvent::Leave)
{
QApplication::restoreOverrideCursor();
qDebug() << "leave";
}
}
return true;
} else {
return QWidget::eventFilter(obj, event);
}
}

anda_skoa
22nd December 2015, 13:15
Your event filter is filtering out all events for the button (returning true).
So it will not get the show event or paint events, etc.

Cheers,
_

jimbo
22nd December 2015, 14:28
Hello anda_skoa,

Many thanks.
I must spend more time RTFM, than RTFInternet.

Regards