Disable clear button in read-only QLineEdit.

Task-number: QTBUG-34315
Change-Id: I6c318879aee907c080e871a541da4ba5eadd71ed
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Friedemann Kleint 2013-11-01 09:17:10 +01:00 committed by The Qt Project
parent 142c5ef9dd
commit 161e8653c3
4 changed files with 16 additions and 0 deletions

View File

@ -495,6 +495,7 @@ void QLineEdit::setClearButtonEnabled(bool enable)
return;
if (enable) {
QAction *clearAction = new QAction(d->clearButtonIcon(), QString(), this);
clearAction->setEnabled(!isReadOnly());
clearAction->setObjectName(QLatin1String(clearButtonActionNameC));
d->addAction(clearAction, 0, QLineEdit::TrailingPosition, QLineEditPrivate::SideWidgetClearButton | QLineEditPrivate::SideWidgetFadeInWithText);
} else {
@ -1336,6 +1337,7 @@ void QLineEdit::setReadOnly(bool enable)
Q_D(QLineEdit);
if (d->control->isReadOnly() != enable) {
d->control->setReadOnly(enable);
d->setClearButtonEnabled(!enable);
setAttribute(Qt::WA_MacShowFocusRect, !enable);
setAttribute(Qt::WA_InputMethodEnabled, d->shouldEnableInputMethod());
#ifndef QT_NO_CURSOR

View File

@ -376,6 +376,16 @@ QIcon QLineEditPrivate::clearButtonIcon() const
return QIcon(q->style()->standardPixmap(QStyle::SP_LineEditClearButton, &styleOption, q));
}
void QLineEditPrivate::setClearButtonEnabled(bool enabled)
{
foreach (const SideWidgetEntry &e, trailingSideWidgets) {
if (e.flags & SideWidgetClearButton) {
e.action->setEnabled(enabled);
break;
}
}
}
void QLineEditPrivate::positionSideWidgets()
{
Q_Q(QLineEdit);

View File

@ -199,6 +199,7 @@ public:
void removeAction(QAction *action);
QSize iconSize() const;
QIcon clearButtonIcon() const;
void setClearButtonEnabled(bool enabled);
void positionSideWidgets();
inline bool hasSideWidgets() const { return !leadingSideWidgets.isEmpty() || !trailingSideWidgets.isEmpty(); }
inline const SideWidgetEntryList &leftSideWidgetList() const

View File

@ -4097,6 +4097,9 @@ void tst_QLineEdit::clearButton()
QTRY_COMPARE(filterModel->rowCount(), 1); // matches 'ab'
QTest::mouseClick(clearButton, Qt::LeftButton, 0, QRect(QPoint(0, 0), clearButton->size()).center());
QTRY_COMPARE(filterModel->rowCount(), 3);
filterLineEdit->setReadOnly(true); // QTBUG-34315
QVERIFY(!clearButton->isEnabled());
}
void tst_QLineEdit::sideWidgets()