简体   繁体   中英

How to specifically connect to overloaded signals?

I am using signal with 2 overloads

buttonClicked = pyqtSignal([int],[str])

I want to connect only one overload(int) with a slot. Whenever I call the emit the other overload(str) I want nothing to happen. How to achieve this?

class Example(QWidget):

    buttonClicked = pyqtSignal([int],[str])

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.btn = QPushButton('Button',self)
        self.btn.clicked.connect(self.doAction)
        self.make_conn()
        self.setWindowTitle('Yo')
        self.show()

    def make_conn(self):
        self.buttonClicked.connect(self.showDialog) #How to make specific connection here . Using self.buttonClicked[int].connect(self.showDialog) doesnt work.

    def showDialog(self):
        print('here')

    def doAction(self):
        self.buttonClicked.emit('soru') #should NOT call showDialog
        self.buttonClicked.emit(23) #should call showDialog

Ok I searched the web and I somehow found a solution and some interesting things.

First, when using emit() I have to specify the overload by specifying the type.

For example in my above example if I want to emit the signal for str version I have to call self.buttonClicked[str].emit('soru') . Secondly, I have to specify the overloaded version detail by telling it if its str or int when connecting the signal with the slot. Like self.buttonClicked[str].connect(showDialog) .

So if now I emit 2 signals specifically:

self.buttonClicked[str].emit('soru')
self.buttonClicked[int].emit(23)

Then only str version will call showDialog. Now I do not specify the overloaded version when connecting like:

self.buttonClicked.connect(showDialog)

Then only the overloaded version which was specified first when creating the pyqtSignal([int],[str]) will be called. So here, only 'int' version will be connected to the slot.

Source: source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM