[PyQt] Using new-style connections with QSignalMapper

Phil Thompson phil at riverbankcomputing.com
Tue Mar 9 12:43:39 GMT 2010


On Mon, 08 Mar 2010 17:29:48 -0500, Mark Visser <markv at lumierevfx.com>
wrote:
> Is there some reason new-style connection from any signal to 
> QSignalMapper.map do not appear to work?
> 
> Using old-style self.connect works, i.e.:
> |
>         #self.connect(self.b1, SIGNAL("clicked()"),
>         #             self.mapper, SLOT("map()"))
>         #self.connect(self.b2, SIGNAL("clicked()"),
>         #             self.mapper, SLOT("map()"))
> |
> But using new-style self.<signal>.connect does not:
> 
> |||        self.b1.clicked.connect(self.mapper.map)
>         self.b2.clicked.connect(self.mapper.map)
> |
> Here's a complete example:|
> =========================================
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> app = QApplication([])
> 
> class Test(QWidget):
>     def __init__(self, parent=None):
>         super(Test, self).__init__(parent)
>         layout = QVBoxLayout()
>        
>         self.b1 = QPushButton("B1")
>         self.b2 = QPushButton("B2")
> 
>         layout.addWidget(self.b1)
>         layout.addWidget(self.b2)
> 
>         self.setLayout(layout)
> 
>         self.mapper = QSignalMapper(self)
> 
>         self.mapper.setMapping(self.b1, "B1")
>         self.mapper.setMapping(self.b2, "B2")
> 
>         self.b1.clicked.connect(self.mapper.map)
>         self.b2.clicked.connect(self.mapper.map)
> 
>         #self.connect(self.b1, SIGNAL("clicked()"),
>         #             self.mapper, SLOT("map()"))
>         #self.connect(self.b2, SIGNAL("clicked()"),
>         #             self.mapper, SLOT("map()"))
> 
>         self.mapper.mapped[QString].connect(self.sayIt)
> 
>     @pyqtSlot(QString)
>     def sayIt(self, name):
>         print name
>        
> 
> t = Test()
> 
> t.show()
> 
> app.exec_()
> ||=========================================|

It's actually a problem with QSignalMapper.map() being called from a proxy
rather than new-style connections.

The workaround is to explicitly specify a signal that is compatible with
map()...

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

Tonight's PyQt snapshot will be smarter about finding a usable Qt slot
before deciding that it needs to use a proxy so that the workaround won't
be necessary.

Thanks,
Phil


More information about the PyQt mailing list