How to bind a function to an Action from Qt menubar?(如何将函数绑定到 Qt 菜单栏中的操作?)
问题描述
我正在使用 Python3 和 PyQt5,在 Qt Designer 中制作我的小部件和窗口.更重要的是,我不会从 .ui 生成 .py 文件.我只是使用下一个代码加载它:
I'm using Python3 and PyQt5, make my widgets and windows in Qt Designer. What is more, I do not generate .py files from a .ui. I simply load it using next code:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
所以,我想知道,如何将菜单栏操作绑定到函数.
So, I wanted to know, how do I bind menu bar actions to functions.
有什么办法可以做这样的事情吗?
Is there any way I can do something like this?
self.getActionByName("actionTest_Action").connect(self.do_something)
推荐答案
在使用loadUi的时候不需要使用findChild,因为该方法以objectName为名称将对象添加到类的属性中,例如在这个比另一个答案更简洁的代码是:
It is not necessary to use findChild when using loadUi since this method adds the object to the attributes of the class using the objectName as a name, for example in this particular case a cleaner code than the other answer is:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
self.actionTest_Action.triggered.connect(self.test)
def test(self):
print("Test")
这篇关于如何将函数绑定到 Qt 菜单栏中的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将函数绑定到 Qt 菜单栏中的操作?


基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01