pyqt: A correct way to connect multiple signals to the same function in pyqt (QSignalMapper not applicable)

前端 未结 2 1476
感情败类
感情败类 2021-01-03 05:23
  1. I\'ve ready many posts on how to connect multiple signals to the same event handler in python and pyqt. For example, connecting several buttons or comboboxes to the

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 06:12

    in many cases the parameter carried by signal can be catched in another way, e.g. if an objectName is set for the sending object, so QSignalMapper can be used:

        self.signalMapper = QtCore.QSignalMapper(self)
        self.signalMapper.mapped[str].connect(myFunction)  
    
        self.combo.currentIndexChanged.connect(self.signalMapper.map)
        self.signalMapper.setMapping(self.combo, self.combo.objectName())
    
       def myFunction(self, identifier):
             combo = self.findChild(QtGui.QComboBox,identifier)
             index = combo.currentIndex()
             text = combo.currentText()
             data = combo.currentData()
    

提交回复
热议问题