Do not accept key events if a widget is disabled

The disabled state was handled in qapplication_xxx.cpp before.
As the platform integration only knows about windows and
not widgets the state check is now done in qwidget. This commit
just adds key events to the list of events which are ignored
if the widget is disabled. This list also contains mouse events
for example.

Task-number: QTBUG-27417

Change-Id: I55949e1c1aaa992ba71df51c5b5e8177ec6f1e86
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Oliver Wolff 2012-10-04 15:02:48 +02:00 committed by The Qt Project
parent 9ab8c0ae98
commit 57fac2e83a
2 changed files with 42 additions and 1 deletions

View File

@ -7807,7 +7807,7 @@ bool QWidget::event(QEvent *event)
{
Q_D(QWidget);
// ignore mouse events when disabled
// ignore mouse and key events when disabled
if (!isEnabled()) {
switch(event->type()) {
case QEvent::TabletPress:
@ -7822,6 +7822,8 @@ bool QWidget::event(QEvent *event)
case QEvent::TouchEnd:
case QEvent::TouchCancel:
case QEvent::ContextMenu:
case QEvent::KeyPress:
case QEvent::KeyRelease:
#ifndef QT_NO_WHEELEVENT
case QEvent::Wheel:
#endif

View File

@ -174,6 +174,8 @@ private slots:
void palettePropagation();
void palettePropagation2();
void enabledPropagation();
void ignoreKeyEventsWhenDisabled_QTBUG27417();
void properTabHandlingWhenDisabled_QTBUG27417();
void popupEnterLeave();
#ifndef QT_NO_DRAGANDDROP
void acceptDropsPropagation();
@ -1053,6 +1055,43 @@ void tst_QWidget::enabledPropagation()
QVERIFY( !grandChildWidget->isEnabled() );
}
void tst_QWidget::ignoreKeyEventsWhenDisabled_QTBUG27417()
{
QLineEdit lineEdit;
lineEdit.setDisabled(true);
lineEdit.show();
QTest::keyClick(&lineEdit, Qt::Key_A);
QTRY_VERIFY(lineEdit.text().isEmpty());
}
void tst_QWidget::properTabHandlingWhenDisabled_QTBUG27417()
{
QWidget widget;
QVBoxLayout *layout = new QVBoxLayout();
QLineEdit *lineEdit = new QLineEdit();
layout->addWidget(lineEdit);
QLineEdit *lineEdit2 = new QLineEdit();
layout->addWidget(lineEdit2);
QLineEdit *lineEdit3 = new QLineEdit();
layout->addWidget(lineEdit3);
widget.setLayout(layout);
widget.show();
lineEdit->setFocus();
QTRY_VERIFY(lineEdit->hasFocus());
QTest::keyClick(&widget, Qt::Key_Tab);
QTRY_VERIFY(lineEdit2->hasFocus());
QTest::keyClick(&widget, Qt::Key_Tab);
QTRY_VERIFY(lineEdit3->hasFocus());
lineEdit2->setDisabled(true);
lineEdit->setFocus();
QTRY_VERIFY(lineEdit->hasFocus());
QTest::keyClick(&widget, Qt::Key_Tab);
QTRY_VERIFY(!lineEdit2->hasFocus());
QVERIFY(lineEdit3->hasFocus());
}
// Drag'n drop disabled in this build.
#ifndef QT_NO_DRAGANDDROP
void tst_QWidget::acceptDropsPropagation()