python - 如何在 pyqt 中为 QAction 重新实现 triggered() 信号?

标签 python plugins pyqt signals-slots qaction

我将 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/

相关文章:

python - 如何将命令提示符中的 Python 编码保存为文件?

python - 使用 "python -i"交互解释时如何重新加载我的 Python 源文件

php - 将 PHP 变量传递给 jQuery 函数

javascript - 我的 Raphael "text-overflow: ellipsis;"实现插件是否矫枉过正?

python - 将 numpy 图像转换为 QPixmap

python - 如何获取 QComboBox 的当前文本内容?

python - PyQt4 强制 View 从 QAbstractItemModel 获取更多

python - 列表交集的索引

python - 如何克隆 Django 模型实例对象并将其保存到数据库中?

ios - 由于 es6-prepare-plugin,安装插件总是失败