diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2013-07-17 10:38:56 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-08 12:55:48 +0200 |
commit | bff5100177a2e1fb4f685ce72f935484ab027203 (patch) | |
tree | 24b5fb36a855549bb263810a7efbab5b0f7517f2 /src/widgets/widgets/qlineedit.cpp | |
parent | bc87acaa1f72d907c68aa6fdb1586191eee8aead (diff) |
Add clearButton-property to QLineEdit.
Based on addAction-API.
Change-Id: Ie6c3d2d728b23a85cdd80428c92ee8208ae0a65c
Done-with: Kevin.Ottens@kdab.com
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
Diffstat (limited to 'src/widgets/widgets/qlineedit.cpp')
-rw-r--r-- | src/widgets/widgets/qlineedit.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index c704eb5c35..a8b6b1a10c 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -475,6 +475,42 @@ QAction *QLineEdit::addAction(const QIcon &icon, ActionPosition position) return result; } +/*! + \property QLineEdit::clearButtonEnabled + \brief Whether the line edit displays a clear button when it is not empty. + + If enabled, the line edit displays a trailing \e clear button when it contains + some text, otherwise the line edit does not show a clear button (the + default). + + \sa addAction(), removeAction() + \since 5.2 +*/ + +static const char clearButtonActionNameC[] = "_q_qlineeditclearaction"; + +void QLineEdit::setClearButtonEnabled(bool enable) +{ + Q_D(QLineEdit); + if (enable == isClearButtonEnabled()) + return; + if (enable) { + QAction *clearAction = new QAction(d->clearButtonIcon(), QString(), this); + clearAction->setObjectName(QLatin1String(clearButtonActionNameC)); + d->addAction(clearAction, 0, QLineEdit::TrailingPosition, QLineEditPrivate::SideWidgetClearButton | QLineEditPrivate::SideWidgetFadeInWithText); + } else { + QAction *clearAction = findChild<QAction *>(QLatin1String(clearButtonActionNameC)); + Q_ASSERT(clearAction); + removeAction(clearAction); + delete clearAction; + } +} + +bool QLineEdit::isClearButtonEnabled() const +{ + return findChild<QAction *>(QLatin1String(clearButtonActionNameC)); +} + void QLineEdit::setFrame(bool enable) { Q_D(QLineEdit); @@ -2143,6 +2179,9 @@ void QLineEdit::changeEvent(QEvent *ev) update(); break; case QEvent::LayoutDirectionChange: + foreach (const QLineEditPrivate::SideWidgetEntry &e, d->trailingSideWidgets) // Refresh icon to show arrow in right direction. + if (e.flags & QLineEditPrivate::SideWidgetClearButton) + static_cast<QLineEditIconButton *>(e.widget)->setIcon(d->clearButtonIcon()); d->positionSideWidgets(); break; default: |