diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp index 7c5ba79cb6..e1c7a4e280 100644 --- a/src/widgets/widgets/qlineedit_p.cpp +++ b/src/widgets/widgets/qlineedit_p.cpp @@ -533,6 +533,17 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE return nullptr; #endif } + + // QTBUG-59957: clear button should be the leftmost action. + if (!before && !(flags & SideWidgetClearButton) && position == QLineEdit::TrailingPosition) { + for (const SideWidgetEntry &e : trailingSideWidgets) { + if (e.flags & SideWidgetClearButton) { + before = e.action; + break; + } + } + } + // If there is a 'before' action, it takes preference // There's a bug in GHS compiler that causes internal error on the following code. diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 330ce3a836..b7628b30e9 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -310,6 +310,7 @@ private slots: void shortcutOverrideOnReadonlyLineEdit_data(); void shortcutOverrideOnReadonlyLineEdit(); + void QTBUG59957_clearButtonLeftmostAction(); protected slots: void editingFinished(); @@ -4612,5 +4613,27 @@ void tst_QLineEdit::shortcutOverrideOnReadonlyLineEdit() QCOMPARE(spy.count(), activationCount); } +void tst_QLineEdit::QTBUG59957_clearButtonLeftmostAction() +{ + QLineEdit lineEdit; + lineEdit.setClearButtonEnabled(true); + + auto clearButton = lineEdit.findChild(); + QVERIFY(clearButton); + + QPixmap pixmap(16, 16); + lineEdit.addAction(QIcon(pixmap), QLineEdit::TrailingPosition); + lineEdit.addAction(QIcon(pixmap), QLineEdit::TrailingPosition); + + lineEdit.show(); + + const auto buttons = lineEdit.findChildren(); + for (const auto button : buttons) { + if (button == clearButton) + continue; + QVERIFY(clearButton->x() < button->x()); + } +} + QTEST_MAIN(tst_QLineEdit) #include "tst_qlineedit.moc"