【发布时间】:2012-02-08 02:33:14
【问题描述】:
我在工具栏中有大约 10 个 QAction(这个数字在运行时会有所不同),它们都会做同样的事情,但使用不同的参数。我正在考虑将参数作为属性添加到 QAction 对象,然后,QAction 的触发信号也会将对象本身发送到回调函数,以便我可以获得该函数所需的参数。我实际上有两个问题:
- 可以吗?
- 有更好的方法吗?
【问题讨论】:
标签: python pyqt signals-slots
我在工具栏中有大约 10 个 QAction(这个数字在运行时会有所不同),它们都会做同样的事情,但使用不同的参数。我正在考虑将参数作为属性添加到 QAction 对象,然后,QAction 的触发信号也会将对象本身发送到回调函数,以便我可以获得该函数所需的参数。我实际上有两个问题:
【问题讨论】:
标签: python pyqt signals-slots
如何在 PyQt 中将参数传递给回调函数
您可以使用标准 Python 库中的 functools.partial。以QAction 为例:
some_action.triggered.connect(functools.partial(some_callback, param1, param2))
【讨论】:
您可以使用与 GUI 控件插槽关联的 lambda 函数将额外参数传递给您要执行的方法。
# Create the build button with its caption
self.build_button = QPushButton('&Build Greeting', self)
# Connect the button's clicked signal to AddControl
self.build_button.clicked.connect(lambda: self.AddControl('fooData'))
def AddControl(self, name):
print name
来源:snip2code - Using Lambda Function To Pass Extra Argument in PyQt4
【讨论】:
传递参数的最佳方式是根本不传递它们。您可以使用 python 的动态特性,将所需的任何数据设置为小部件本身的属性,使用self.sender() 在处理程序中获取目标小部件,然后直接从小部件获取所需的任何属性。
在此示例中,创建了五个按钮,并将按钮小部件上所需的状态设置为 my_own_data 属性:
import sys
from PyQt4 import QtGui, QtCore
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.centralwidget = QtGui.QWidget()
self.vboxlayout = QtGui.QVBoxLayout()
for idx in range(5):
button = QtGui.QPushButton('button ' + str(idx), None)
button.my_own_data = str(idx) # <<< set your own property
button.clicked.connect(self.click_handler) # <<< no args needed
self.vboxlayout.addWidget(button)
self.centralwidget.setLayout(self.vboxlayout)
self.setCentralWidget(self.centralwidget)
self.show()
def click_handler(self, data=None):
if not data:
target = self.sender() # <<< get the event target, i.e. the button widget
data = target.my_own_data # <<< get your own property
QtGui.QMessageBox.information(self,
"you clicked me!",
"my index is {0}".format(data))
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
【讨论】:
click_handler 不能被连接到它的特定按钮以外的任何东西安全地调用。
click_handler()可以以更通用的方式使用。
您可以使用signal mapper 发送操作对象本身。但是,最好只发送一个标识符并在信号处理程序中完成所有工作。
这是一个简单的演示脚本:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.mapper = QtCore.QSignalMapper(self)
self.toolbar = self.addToolBar('Foo')
self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
for text in 'One Two Three'.split():
action = QtGui.QAction(text, self)
self.mapper.setMapping(action, text)
action.triggered.connect(self.mapper.map)
self.toolbar.addAction(action)
self.mapper.mapped['QString'].connect(self.handleButton)
self.edit = QtGui.QLineEdit(self)
self.setCentralWidget(self.edit)
def handleButton(self, identifier):
if identifier == 'One':
text = 'Do This'
elif identifier == 'Two':
text = 'Do That'
elif identifier == 'Three':
text = 'Do Other'
self.edit.setText(text)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(300, 60)
window.show()
sys.exit(app.exec_())
【讨论】:
QSignalMapper 没有被广泛使用吗?
lambda/partial 解决方案。曾经有一段时间 QSignalMapper 更有用,因为 PyQt 并不总是支持使用 lambda/partial 作为插槽 - 但那是很久以前的事了。不过,目前我想不出它可能更有效的场景(当然,在 C++ 中除外)。
PyQt 使用 QSignalMapper 的方式仍然存在错误。我在这里发现了一个解决方法:
http://www.riverbankcomputing.com/pipermail/pyqt/2010-March/026113.html
要解决已回答的问题,请进行以下更改:
action.triggered[()].connect(self.mapper.map)
以下版本的 Python 需要此功能:
Python 2.6.6 (r266:84292, Feb 21 2013, 19:26:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
【讨论】: