【问题标题】:How to add QAction to QLineEdit in pyqt5?如何在pyqt5中将QAction添加到QLineEdit?
【发布时间】:2021-07-19 18:57:54
【问题描述】:

我正在 pyqt5 中创建一个应用程序,其中我必须将 QAction 添加到 QLineEdit。我尝试了很多次,但都失败了。 这就是我想要的
请帮我。提前谢谢你

【问题讨论】:

  • 请展示您的尝试。 documentation 似乎相当清晰。
  • 那些是 C 的,但我使用的是 python
  • 无论我尝试过什么都行不通
  • 什么你试过了吗? “无论我尝试什么”都是完全没有意义的。该文档针对的是 C++,而不是 C,而且无论如何,在 99% 的情况下,Python 的函数都是相同的。

标签: python python-3.x qt pyqt pyqt5


【解决方案1】:

这是一个非常基本的例子:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    相关资源
    最近更新 更多