我在工具栏中有大约10个 QAction(这个数字在运行时会变化),它们都会做相同的事情,但使用不同的参数。我考虑将参数作为 QAction 对象的属性添加,并且 QAction 的触发信号也会将对象本身发送到回调函数中,以便我可以获取所需的函数参数。关于此,我实际上有两个问题:
- 是否可行?
- 有更好的方法吗?
我在工具栏中有大约10个 QAction(这个数字在运行时会变化),它们都会做相同的事情,但使用不同的参数。我考虑将参数作为 QAction 对象的属性添加,并且 QAction 的触发信号也会将对象本身发送到回调函数中,以便我可以获取所需的函数参数。关于此,我实际上有两个问题:
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
传递参数的最佳方式是根本不需要传递它们。您可以利用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_())
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
不常用吗? - 101QSignalMapper
更有用,因为PyQt并不总是支持使用lambda/partial
作为槽函数 - 但那是很久以前的事了。目前,我无法想象还有哪种情况下它可能更有效(当然,在C++中除外)。 - ekhumoroPyQt在使用QSignalMapper时仍存在一个错误。我在这里发现了一个解决方法:
http://www.riverbankcomputing.com/pipermail/pyqt/2010-March/026113.html
为了修复已回答的问题,请进行以下更改: action.triggered[()].connect(self.mapper.map)
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
click_handler
只能被连接到它的特定按钮安全地调用。 - ekhumoroclick_handler()
可以以更通用的方式使用。 - ccpizza