我在工具栏中有大约10个QAction(这个数字在运行时会有所不同),它们都会做同样的事情,但使用不同的参数。我在考虑将参数作为属性添加到QAction对象,然后,QAction的触发信号也会将对象本身发送到回调函数,以便我可以获取该函数所需的参数。我其实有两个问题:如何将参数传递给PyQt中的回调函数
- 可以这样做吗?
- 有没有更好的方法来做到这一点?
我在工具栏中有大约10个QAction(这个数字在运行时会有所不同),它们都会做同样的事情,但使用不同的参数。我在考虑将参数作为属性添加到QAction对象,然后,QAction的触发信号也会将对象本身发送到回调函数,以便我可以获取该函数所需的参数。我其实有两个问题:如何将参数传递给PyQt中的回调函数
您可以使用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_())
如何参数传递给回调函数在PyQt的
您可以使用functools.partial
从非标准Python库。例如与QAction
:
some_action.triggered.connect(functools.partial(some_callback, param1, param2))
仍有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
您可以使用与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
谢谢!正是我在找什么! – 2014-01-07 08:34:57
来传递参数的最佳方式是无法通过它们。您可以使用python的动态特性,并将所需的任何数据设置为您自己的小部件本身的属性,使用self.sender()
获取处理程序中的目标小部件,然后直接从小部件获取所需的任何属性。
在这个例子中被创建五个按钮和所需的状态被设置在按钮上widget作为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_())
这个答案似乎不是很受欢迎。 QSignalMapper没有被广泛使用吗? – 101 2015-07-11 08:33:37
@ figs。我不知道为什么OP接受了这个答案,因为已经给出了更传统的“lambda/partial”解决方案。曾经有一次'QSignalMapper'更有用,因为PyQt并不总是支持使用'lambda/partial'作为插槽 - 但那是很久以前的事了。然而,目前,我想不出一个更有效的场景(当然,除了C++之外)。 – ekhumoro 2015-07-11 09:45:49