I have a QSignalMapper, and at some moment I need to disconnect this QSignalMapper to a slot, and after that, I reconnect again.
What is a proper way to do this?
Or is there any way so I can check if a QSignalMapper connected to any slot?
I have a QSignalMapper, and at some moment I need to disconnect this QSignalMapper to a slot, and after that, I reconnect again.
What is a proper way to do this?
Or is there any way so I can check if a QSignalMapper connected to any slot?
Instead of connecting and disconnecting the slot one simple solution is to block the emission of the signal using the blockSignals() method.
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._mapper = QtCore.QSignalMapper(self)
vlay = QtWidgets.QVBoxLayout(self)
checkbox = QtWidgets.QCheckBox("Block Signals")
checkbox.stateChanged.connect(self.onStateChanged)
vlay.addWidget(checkbox)
for i in range(5):
button = QtWidgets.QPushButton("{}".format(i))
button.clicked.connect(self._mapper.map)
self._mapper.setMapping(button, "button-{}".format(i))
vlay.addWidget(button)
self._mapper.mapped[str].connect(self.onClicked)
@QtCore.pyqtSlot(int)
def onStateChanged(self, state):
self._mapper.blockSignals(state == QtCore.Qt.Checked)
@QtCore.pyqtSlot(str)
def onClicked(self, text):
print(text)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
On the other hand QSignalMapper is deprecated from Qt 5.10 so in the future it will be eliminated.