PyQt and QSignalMapper/lambdas - multiple signals, single slot

≡放荡痞女 提交于 2019-12-01 10:42:06

问题


I have a list of actions on a menu in PyQt, one for each different feed I want to display. So I have a Y that sets the active feed to Y, Z sets it to Z, etc. (For a webcomic reading program).

I have each on the menu, and felt that an automated approach might be better; rather than typing out each time. Something like a function that adds it to a dictionary, then connects it up with a signal for each to a single slot.

However, I want that slot function, say it's called Foo, to take a parameter to decide what has been clicked. So if X was clicked, then X, Y passes Y, etc.

Looked around, and one SO question said to use a lambda, which didn't look...right somehow.

The other way was with QSignalMapper. I tried looking for this, but couldn't find an example of how to use it.

Can anyone help?

Thanks!


回答1:


You can use functools.partial (link to the documentation):

import functools
...

# note that these are the 'new style' slot connections (not necessarily needed)
self.menu_entry_x.triggered.connect(functools.partial(myfunc, x))
self.menu_entry_y.triggered.connect(functools.partial(myfunc, y))

The example is above is very basic, but I could elaborate if you have more questions.



来源:https://stackoverflow.com/questions/3893876/pyqt-and-qsignalmapper-lambdas-multiple-signals-single-slot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!