Question

I want to check the users input in several QtGui.QLineEdits by the same function with different parameters. I tried QtCore.SignalMapper. This is my code in a test-application:

    self.signalMapper = QtCore.QSignalMapper(self)
    QtCore.QObject.connect(self.lineEdit_331, QtCore.SIGNAL(_fromUtf8('returnPressed()')), self.signalMapper.map)
    QtCore.QObject.connect(self.lineEdit_341, QtCore.SIGNAL(_fromUtf8("returnPressed()")), self.signalMapper.map)
    self.signalMapper.setMapping(self.lineEdit_331,'links')
    self.signalMapper.setMapping(self.lineEdit_341,'rechts')
    QtCore.QObject.connect(self.signalMapper, QtCore.SIGNAL(_fromUtf8("mapped(QString)")),self.test)

The signalMapper exists and all connects return 'True' but the slot isn't called (the same after changing the order of 'connect' and 'setMapping'). Connecting the lineEdits signals to the slot works:

    QtCore.QObject.connect(self.lineEdit_331, QtCore.SIGNAL(_fromUtf8("returnPressed()")), self.test_1)

what's wrong in my code? Thanks for help

Was it helpful?

Solution

The main thing wrong with your code, is that you are using the ugly, error-prone, old-style syntax for connecting signals, instead of the new-style syntax.

Here is a re-write of your example code:

    self.signalMapper = QtCore.QSignalMapper(self)
    self.lineEdit_331.returnPressed.connect(self.signalMapper.map)
    self.lineEdit_331.returnPressed.connect(self.signalMapper.map)
    self.signalMapper.setMapping(self.lineEdit_331, 'links')
    self.signalMapper.setMapping(self.lineEdit_341, 'rechts')
    self.signalMapper.mapped[str].connect(self.test)

If you're curious why your original code didn't work, it's because you should have used SLOT in the first two connections. It should have been:

    QtCore.QObject.connect(
        self.lineEdit_331, QtCore.SIGNAL('returnPressed()'),
        self.signalMapper, QtCore.SLOT('map()'))

This is because there are two overloads of QSignalMapper.map, so you would have needed to specify which one you intended to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top