qtawesome
import sys
from PyQt5.QtWidgets import (QLineEdit, QApplication, QWidget, QVBoxLayout)
import qtawesome as qt
class Widget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.flag = 0
self.layout = QVBoxLayout()
self.line_edit = QLineEdit()
self.line_edit.setClearButtonEnabled(False)
self.line_edit.textChanged.connect(self._on_line_edit_text_changed)
self.clear_icon = qt.icon('mdi.delete-circle-outline', color='gray', color_active='black')
self.clear_action = None
self.layout.addWidget(self.line_edit)
self.setLayout(self.layout)
def _on_line_edit_text_changed(self):
if self.line_edit and self.line_edit.text():
if not self.clear_action:
self.clear_action = self.line_edit.addAction(self.clear_icon, QLineEdit.TrailingPosition)
self.clear_action.triggered.connect(self._on_clear_clicked)
elif self.clear_action and self.line_edit and not self.line_edit.text():
self.line_edit.removeAction(self.clear_action)
self.clear_action = None
def _on_clear_clicked(self):
self.line_edit.clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())