QDialog之屏蔽Esc键(简单深刻,要么重写keyPressEvent然后break忽略此事件,要么重写eventFilter然后return,都是为了忽略此事件)

源码分析

    通过查看QDialog的源码,我们很容易会发现keyPressEvent事件中,当按下Esc键时,会默认执行reject()。

void QDialog::keyPressEvent(QKeyEvent *e)
{
    //   Calls reject() if Escape is pressed. Simulates a button
    //   click for the default button if Enter is pressed. Move focus
    //   for the arrow keys. Ignore the rest.
#ifdef Q_OS_MAC
    if(e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) {
        reject();
    } else
#endif
    if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return: {
            QList<QPushButton*> list = findChildren<QPushButton*>();
            for (int i=0; i<list.size(); ++i) {
                QPushButton *pb = list.at(i);
                if (pb->isDefault() && pb->isVisible()) {
                    if (pb->isEnabled())
                        pb->click();
                    return;
                }
            }
        }
        break;
        case Qt::Key_Escape:
            reject();
            break;
        default:
            e->ignore();
            return;
        }
    } else {
        e->ignore();
    }
}

  Ok,我们如果想改变Esc键的默认动作,则可以通过两种途径:

  ·重写Esc键对应的事件

  ·重写reject()


事件过滤器

   对QDialog使用事件过滤器,过滤Esc键。

      installEventFilter(new EventFilter(this));

bool EventFilter::eventFilter(QObject *obj, QEvent *event)
{
    QDialog *pDialog = qobject_cast<QDialog *>(obj);
    if (pDialog != NULL)
    {
        switch (event->type())
        {
        case QEvent::KeyPress:
        {
            QKeyEvent *pKeyEvent = static_cast<QKeyEvent*>(event);
            if (pKeyEvent->key() == Qt::Key_Escape)
            {
                return true;
            }
        }
        }
    }
    return QObject::eventFilter(obj, event);
}

事件重写

   重写QDialog的键盘事件keyPressEvent。

void Dialog::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Escape:
        break;
    default:
        QDialog::keyPressEvent(event);
    }
}

重写reject

   m_bClosed为关闭的条件,为true时,窗口才会关闭。

void Dialog::reject()
{
    if (m_bClosed)
        QDialog::reject();
}

   关于事件过滤器和事件重写其实是属于一种情况,都是基于事件判断和过滤的,而事件过滤器相对来说更易用、扩展性更好,不需要针对每个控件都去重写对应的事件。

猜你喜欢

转载自blog.csdn.net/lengyuezuixue/article/details/81029344