【问题标题】:Trouble Understanding Signal Mapper PyQt难以理解信号映射器 PyQt
【发布时间】:2015-05-26 03:45:46
【问题描述】:

所以我正在根据我系统上的一些文件生成一个选项菜单。我有一个对象列表,我需要在菜单中动态生成一个选项,并且需要能够让正在创建的函数知道要使用哪个对象。经过一番研究,我找到了下面的帖子。由于我的代表还不高,我无法发表评论:How to pass arguments to callback functions in PyQt

当我运行它时,信号映射器无法正常工作。它甚至没有正确调用handleButton。关于如何正确使用信号映射器的任何想法?

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):
        print 'run'
        if identifier == 'One':
            text = 'Do This'
            print 'Do One'
        elif identifier == 'Two':
            text = 'Do That'
            print 'Do Two'
        elif identifier == 'Three':
            print 'Do 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_())

编辑:

我发现通过使用旧式信号/插槽连接可以解决此问题:

#action.triggered.connect(self.mapper.map)
self.connect(action, QtCore.SIGNAL("triggered()"), self.mapper, QtCore.SLOT("map()"))

#self.mapper.mapped['QString'].connect(self.handleButton)
self.connect(self.mapper, QtCore.SIGNAL("mapped(const QString &)"), self.handleButton)

我是否错误地使用了新式连接?

根据我发布的this postoriginal link,我认为我做的事情是正确的。

【问题讨论】:

    标签: python pyqt qsignalmapper


    【解决方案1】:

    原始示例代码(我编写的)对我使用 Python2 或 Python3 以及几个不同的最新版本 PyQt4 非常有效。但是,如果我使用真正旧版本的 PyQt4 (4.7),则不再调用处理程序。

    原因(和解决方案)在您链接到的邮件列表帖子的回复中给出:

    实际上是 QSignalMapper.map() 从 代理而不是新型连接。

    解决方法是明确指定兼容的信号 用地图()...

    self.b1.clicked[()].connect(self.mapper.map)
    

    今晚的 PyQt 快照将更聪明地寻找可用的 Qt 插槽 在决定它需要使用代理之前,以便解决方法 没有必要。

    有一些信号(如clickedtriggered)总是发送一个默认值,除非你明确要求。对于旧式信号,您可以使用SIGNAL("triggered()") 指定不默认重载它,但对于新式信号,您必须这样做:

        action.triggered[()].connect(self.mapper.map)
    

    但这仅对非常旧版本的 PyQt4 是必需的 - 基本问题早在 2010 年就已修复(不知道确切版本,但 4.8 应该没问题)。

    【讨论】:

    • 感谢@ekhumoro 将尝试更新安装
    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多