QLineEdit: show the clear button if it gets enabled after setting a text

We were fetching "lastText" too late, and setting the opacity
of the clear button to 0.

Change-Id: I82c2aea7dab4af4424fb57e12f78d07a0374457e
Task-number: QTBUG-45518
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
Giuseppe D'Angelo 2015-05-06 17:00:21 +02:00
parent ea17cc0768
commit a34e9ebc1b
3 changed files with 50 additions and 5 deletions

View File

@ -440,6 +440,10 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
Q_Q(QLineEdit); Q_Q(QLineEdit);
if (!newAction) if (!newAction)
return 0; return 0;
if (!hasSideWidgets()) { // initial setup.
QObject::connect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString)));
lastTextSize = q->text().size();
}
QWidget *w = 0; QWidget *w = 0;
// Store flags about QWidgetAction here since removeAction() may be called from ~QAction, // Store flags about QWidgetAction here since removeAction() may be called from ~QAction,
// in which a qobject_cast<> no longer works. // in which a qobject_cast<> no longer works.
@ -456,10 +460,6 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
toolButton->setDefaultAction(newAction); toolButton->setDefaultAction(newAction);
w = toolButton; w = toolButton;
} }
if (!hasSideWidgets()) { // initial setup.
QObject::connect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString)));
lastTextSize = q->text().size();
}
// If there is a 'before' action, it takes preference // If there is a 'before' action, it takes preference
PositionIndexPair positionIndex = before ? findSideWidget(before) : PositionIndexPair(position, -1); PositionIndexPair positionIndex = before ? findSideWidget(before) : PositionIndexPair(position, -1);
SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;

View File

@ -65,7 +65,7 @@ QT_BEGIN_NAMESPACE
// QLineEditIconButton: This is a simple helper class that represents clickable icons that fade in with text // QLineEditIconButton: This is a simple helper class that represents clickable icons that fade in with text
class QLineEditIconButton : public QToolButton class Q_AUTOTEST_EXPORT QLineEditIconButton : public QToolButton
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity) Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)

View File

@ -304,6 +304,7 @@ private slots:
void undoRedoAndEchoModes(); void undoRedoAndEchoModes();
void clearButton(); void clearButton();
void clearButtonVisibleAfterSettingText_QTBUG_45518();
void sideWidgets(); void sideWidgets();
void shouldShowPlaceholderText_data(); void shouldShowPlaceholderText_data();
@ -4273,6 +4274,50 @@ void tst_QLineEdit::clearButton()
QVERIFY(!clearButton->isEnabled()); QVERIFY(!clearButton->isEnabled());
} }
void tst_QLineEdit::clearButtonVisibleAfterSettingText_QTBUG_45518()
{
#ifndef QT_BUILD_INTERNAL
QSKIP("This test requires a developer build");
#else
QLineEdit edit;
edit.setMinimumWidth(200);
centerOnScreen(&edit);
QLineEditIconButton *clearButton;
clearButton = edit.findChild<QLineEditIconButton *>();
QVERIFY(!clearButton);
edit.setText(QStringLiteral("some text"));
edit.show();
QVERIFY(QTest::qWaitForWindowActive(&edit));
QVERIFY(!edit.isClearButtonEnabled());
clearButton = edit.findChild<QLineEditIconButton *>();
QVERIFY(!clearButton);
edit.setClearButtonEnabled(true);
QVERIFY(edit.isClearButtonEnabled());
clearButton = edit.findChild<QLineEditIconButton *>();
QVERIFY(clearButton);
QVERIFY(clearButton->isVisible());
QTRY_VERIFY(clearButton->opacity() > 0);
QTRY_COMPARE(clearButton->cursor().shape(), Qt::ArrowCursor);
QTest::mouseClick(clearButton, Qt::LeftButton, 0, clearButton->rect().center());
QTRY_COMPARE(edit.text(), QString());
QTRY_COMPARE(clearButton->opacity(), qreal(0));
QTRY_COMPARE(clearButton->cursor().shape(), clearButton->parentWidget()->cursor().shape());
edit.setClearButtonEnabled(false);
QVERIFY(!edit.isClearButtonEnabled());
clearButton = edit.findChild<QLineEditIconButton *>();
QVERIFY(!clearButton);
#endif // QT_BUILD_INTERNAL
}
void tst_QLineEdit::sideWidgets() void tst_QLineEdit::sideWidgets()
{ {
QWidget testWidget; QWidget testWidget;