Skip to content
Advertisement

How to add QAction to QLineEdit in pyqt5?

I am making an application in pyqt5 in which I have to add QAction to QLineEdit. I have tried many times but failed. GitHub issues This is what I want
Please help me. Thank you in advance

Advertisement

Answer

Here is a very basic example:

from PyQt5 import QtWidgets

app = QtWidgets.QApplication([])
# create line edit
line_edit = QtWidgets.QLineEdit()
# create icon. Here I chose a random standard icon but you can create your own from one or more images.
icon = app.style().standardIcon(QtWidgets.QStyle.SP_ArrowRight)
# add action to line edit
action = line_edit.addAction(icon, line_edit.TrailingPosition)
# connect action.triggered signal to a slot
action.triggered.connect(lambda: print('action triggered:', line_edit.text()))
# show line edit and start event loop
line_edit.show()
app.exec()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement