在 PyQt 中,如何使用信号和槽将下拉列表连接到函数?
在 PyQt 中,有一种信号和槽的概念,用于将对象连接到彼此的函数,但我似乎找不到它们引用与其他对象不关联的函数。例如,我想要一个下拉列表来运行算法 A 或算法 B。
PyQt是如何实现这个功能的呢?
In PyQt, there's a concept of signals and slots to connect objects to one another's functions, but I can't seem to find them referenced to functions not associated with other objects. For example, I want a dropdown list to have algorithm A or algorithm B run.
How does PyQt accomplish this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要改变下拉列表调用函数的效果吗?
将下拉列表的适当信号连接到您的函数。
例如,使用 QComboBox currentIndexChanged() 信号。将其连接到一个“包装器”函数,该函数决定(基于索引)调用哪个函数。
编辑:包装器可以非常简单,如下所示:
Edit2:如果您想要一些连接插槽的替代方法:
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#connecting-signals-and-slots
注意他们说话时关于 Py 或 Qt 信号,以及当他们谈论 Python 函数或方法时。例如,以下是将 Qt 信号连接到 Python 函数和 Python 方法的语法:
Do you want the effect of changing the drop down list to call a function?
Connect the dropdown list's appropriate signal to your function.
For example with the QComboBox currentIndexChanged() signal. Connect that to a "wrapper" function that decides (based on the index) which function to call.
Edit: The wrapper can be very simple, like so:
Edit2: If you want some alternate methods for connecting slots:
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#connecting-signals-and-slots
Note when they are talking about Py or Qt signals, and when they are talking about Python functions or methods. E.g., these are the syntax for connecting a Qt signal to a Python function and a Python method: