I am making an application in pyqt5 in which I have to add QAction to QLineEdit. I have tried many times but failed.
This is what I want
Please help me. Thank you in advance
Advertisement
Answer
Here is a very basic example:
JavaScript
x
15
15
1
from PyQt5 import QtWidgets
2
3
app = QtWidgets.QApplication([])
4
# create line edit
5
line_edit = QtWidgets.QLineEdit()
6
# create icon. Here I chose a random standard icon but you can create your own from one or more images.
7
icon = app.style().standardIcon(QtWidgets.QStyle.SP_ArrowRight)
8
# add action to line edit
9
action = line_edit.addAction(icon, line_edit.TrailingPosition)
10
# connect action.triggered signal to a slot
11
action.triggered.connect(lambda: print('action triggered:', line_edit.text()))
12
# show line edit and start event loop
13
line_edit.show()
14
app.exec()
15