Add ReadOnlyChange event to widgets that support setReadOnly(bool).

This is useful for widget styles to react when widgets are set
read-only, e.g. to update their palette accordingly.

[ChangeLog][QtWidgets] All widgets with a setReadOnly method now
send a ReadOnlyChange event (e.g. for app-specific palette changes)

Change-Id: I74719a3e1b7d034d9bfc94305f846f42aae935bd
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
David Faure 2014-03-07 18:33:50 +01:00 committed by The Qt Project
parent 27016b89ae
commit 73d761ffe5
8 changed files with 27 additions and 2 deletions

View File

@ -192,6 +192,7 @@ QT_BEGIN_NAMESPACE
\value Polish The widget is polished.
\value PolishRequest The widget should be polished.
\value QueryWhatsThis The widget should accept the event if it has "What's This?" help.
\value ReadOnlyChange Widget's read-only state has changed (since Qt 5.4).
\value RequestSoftwareInputPanel A widget wants to open a software input panel (SIP).
\value Resize Widget's size changed (QResizeEvent).
\value ScrollPrepare The object needs to fill in its geometry information (QScrollPrepareEvent).

View File

@ -150,6 +150,8 @@ public:
WindowUnblocked = 104, // windows modal blocking has ended
WindowStateChange = 105,
ReadOnlyChange = 106, // readonly state has changed
ToolTip = 110,
WhatsThis = 111,
StatusTip = 112,

View File

@ -8224,6 +8224,7 @@ bool QWidget::event(QEvent *event)
case QEvent::MacSizeChange:
case QEvent::ContentsRectChange:
case QEvent::ThemeChange:
case QEvent::ReadOnlyChange:
changeEvent(event);
break;
@ -8395,7 +8396,7 @@ bool QWidget::event(QEvent *event)
QEvent::ModifiedChange, QEvent::MouseTrackingChange,
QEvent::ParentChange, QEvent::WindowStateChange,
QEvent::LanguageChange, QEvent::LocaleChange,
QEvent::LayoutDirectionChange.
QEvent::LayoutDirectionChange, QEvent::ReadOnlyChange.
*/
void QWidget::changeEvent(QEvent * event)

View File

@ -315,6 +315,8 @@ void QAbstractSpinBox::setReadOnly(bool enable)
Q_D(QAbstractSpinBox);
d->readOnly = enable;
d->edit->setReadOnly(enable);
QEvent event(QEvent::ReadOnlyChange);
QApplication::sendEvent(this, &event);
update();
}

View File

@ -1348,6 +1348,8 @@ void QLineEdit::setReadOnly(bool enable)
#ifndef QT_NO_CURSOR
setCursor(enable ? Qt::ArrowCursor : Qt::IBeamCursor);
#endif
QEvent event(QEvent::ReadOnlyChange);
QCoreApplication::sendEvent(this, &event);
update();
}
}

View File

@ -2529,6 +2529,8 @@ void QPlainTextEdit::setReadOnly(bool ro)
}
setAttribute(Qt::WA_InputMethodEnabled, shouldEnableInputMethod(this));
d->control->setTextInteractionFlags(flags);
QEvent event(QEvent::ReadOnlyChange);
QApplication::sendEvent(this, &event);
}
/*!

View File

@ -2122,6 +2122,8 @@ void QTextEdit::setReadOnly(bool ro)
}
d->control->setTextInteractionFlags(flags);
setAttribute(Qt::WA_InputMethodEnabled, shouldEnableInputMethod(this));
QEvent event(QEvent::ReadOnlyChange);
QApplication::sendEvent(this, &event);
}
/*!

View File

@ -393,18 +393,31 @@ void tst_QSpinBox::valueChangedHelper(int value)
actualValues << value;
}
class MySpinBox: public QSpinBox
{
public:
MySpinBox(QWidget *parent = 0) : QSpinBox(parent) {}
void changeEvent(QEvent *ev) {
eventsReceived.append(ev->type());
}
QList<QEvent::Type> eventsReceived;
};
void tst_QSpinBox::setReadOnly()
{
QSpinBox spin(0);
MySpinBox spin(0);
spin.show();
QTest::keyClick(&spin, Qt::Key_Up);
QCOMPARE(spin.value(), 1);
spin.setReadOnly(true);
QCOMPARE(spin.eventsReceived, QList<QEvent::Type>() << QEvent::ReadOnlyChange);
QTest::keyClick(&spin, Qt::Key_Up);
QCOMPARE(spin.value(), 1);
spin.stepBy(1);
QCOMPARE(spin.value(), 2);
spin.setReadOnly(false);
QCOMPARE(spin.eventsReceived, QList<QEvent::Type>() << QEvent::ReadOnlyChange << QEvent::ReadOnlyChange);
QTest::keyClick(&spin, Qt::Key_Up);
QCOMPARE(spin.value(), 3);
}