如何在PyQt中将参数传递给回调函数

11

我在工具栏中有大约10个 QAction(这个数字在运行时会变化),它们都会做相同的事情,但使用不同的参数。我考虑将参数作为 QAction 对象的属性添加,并且 QAction 的触发信号也会将对象本身发送到回调函数中,以便我可以获取所需的函数参数。关于此,我实际上有两个问题:

  • 是否可行?
  • 有更好的方法吗?
5个回答

47
如何在PyQt中将参数传递给回调函数
您可以使用Python标准库中的functools.partial。以下是使用QAction的示例:
some_action.triggered.connect(functools.partial(some_callback, param1, param2))

11

您可以使用与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 - 使用Lambda函数在PyQt4中传递额外的参数


4

传递参数的最佳方式是根本不需要传递它们。您可以利用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只能被连接到它的特定按钮安全地调用。 - ekhumoro
@ekhumoro:同意;添加了可选的数据参数,这样 click_handler() 可以以更通用的方式使用。 - ccpizza

3
你可以使用信号映射器发送动作对象本身。然而,仅发送标识符并在信号处理程序中完成所有工作可能更好。
这是一个简单的演示脚本:
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 不常用吗? - 101
@figs. 我不知道为什么原帖作者接受了这个答案,因为更传统的“lambda/partial”解决方案已经被提出。曾经有一段时间,QSignalMapper更有用,因为PyQt并不总是支持使用lambda/partial作为槽函数 - 但那是很久以前的事了。目前,我无法想象还有哪种情况下它可能更有效(当然,在C++中除外)。 - ekhumoro

1

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接