我将 Python 与 PyQt 一起用于我的界面,并使用 Yapsi 来添加插件。 Yapsy 找到了我所有的插件并将它们全部添加到我的主窗口中的菜单中。每个插件都由触发信号激活。 QAction 的这个信号没有参数,我需要知道哪个插件发出了信号。
这是相关代码:
pluginMenu = self.menuBar().addMenu("P&lugins")
# Create plugin manager
self.manager = PluginManager(categories_filter={ "Formatters": Formatter})
self.manager.setPluginPlaces(["plugins"])
# Load plugins
self.manager.locatePlugins()
self.manager.loadPlugins()
# A do-nothing formatter by default
self.formatters = {}
for plugin in self.manager.getPluginsOfCategory("Formatters"):
# plugin.plugin_object is an instance of the plugin
print(plugin.plugin_object.name)
# The method to create action associated each input to default triggered() signal
newAction = self.createAction(plugin.plugin_object.name, slot=self.updatePreview())
self.addActions(pluginMenu, (newAction, None))
self.formatters[plugin.plugin_object.name] = (plugin.plugin_object, newAction)
def updatePreview(self):
# Here I need know what plugin emit the signal
#===================================================================
我想通过一些参数将信号与其他信号连接起来,但我不知道该怎么做。
最佳答案
我不知道什么是 Yapsi,但是有 QObject.sender
方法:
QObject QObject.sender (self)
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. The pointer is valid only during the execution of the slot that calls this function from this object's thread context.
The pointer returned by this function becomes invalid if the sender is destroyed, or if the slot is disconnected from the sender's signal.
Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
Warning: As mentioned above, the return value of this function is not valid when the slot is called via a Qt.DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.
更多提示:http://blog.odnous.net/2011/06/frequently-overlooked-and-practical.html
关于python - 如何在 pyqt 中为 QAction 重新实现 triggered() 信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808435/