문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top