Search code examples
qtqwidgetqpushbuttoneventfilter

Unexpected behavior on QWidget while the event filtering


Just i've tested the following codes on QPushButton and on QWidget and i've encountered with different behaviors. But why?

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->widget->setStyleSheet("background:blue;");
    ui->pushButton->setStyleSheet("background:blue;");
    ui->widget->installEventFilter(this);
    ui->pushButton->installEventFilter(this);

        ...
}

bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
    if (watched==ui->pushButton && event->type()==QEvent::Paint)
    {
        // Do nothing
        return true;
    }
    else if (watched==ui->widget && event->type()==QEvent::Paint)
    {
        // Do nothing
        return true;
    }
    else
        return QMainWindow::eventFilter(watched, event);
}

Then, the pushButton has disappeared as normally, because i've masked its paintEvent with eventFilter. But the widget has painted to blue. Why widget hasn't disappeared.


Solution

  • I've solved the problem with changing QEvent::Paint with QEvent::Polish.

    To who wanted the detailed description

    Thanks everybody.