Connecting an Overloaded PyQT Signal Using New Style Syntax

I create my own widget, which is basically a QGroupBox containing a custom number of QCheckBox buttons, where each of them must control a specific bit in the presented bitmask using QBitArray . To do this, I added QCheckBox instances in the QButtonGroup , each of which the button sets an integer identifier:

def populate(self, num_bits, parent = None): """ Adds check boxes to the GroupBox according to the bitmask size """ self.bitArray.resize(num_bits) layout = QHBoxLayout() for i in range(num_bits): cb = QCheckBox() cb.setText(QString.number(i)) self.buttonGroup.addButton(cb, i) layout.addWidget(cb) self.setLayout(layout) 

Then, every time the user clicks the checkbox contained in self.buttonGroup, I would like self.bitArray to be notified, so the corresponding bit in the array can be set / unset accordingly. To do this, I intended to connect the QButtonGroup buttonClicked (int) signal to QBitArray toggleBit (int), and to be as pyphonic as possible, I wanted to use new-style signals with syntax, so I tried this:

 self.buttonGroup.buttonClicked.connect(self.bitArray.toggleBit) 

The problem is that buttonClicked is an overloaded signal, so there is also a buttonClicked signature (QAbstractButton *). In fact, when the program is running, I get this error when I click on the checkbox:

 The debugged program raised the exception unhandled TypeError "QBitArray.toggleBit(int): argument 1 has unexpected type 'QCheckBox'" 

which clearly shows that the toggleBit method received a buttonClicked (QAbstractButton *) signal instead of a buttonClicked (int) button.

So the question is, how can I specify a signal connection using the new style syntax , so that self.bitArray receives the buttonClicked (int) signal instead of the standard overload - buttonClicked (QAbstractButton *)?

EDIT: The PyQT Signal and Slot Support Documentation New-style states that you can use the pyqtSlot decorators to indicate which signal you want to connect to this slot, but this applies to the slot you create. What to do when the slot is from the ready class? Is this the only option that subclasses it and then overrides this slot using the right decorator?

+6
source share
1 answer

While looking at related questions, I found this answer to be exactly what I need. The correct syntax of the new style signal for connecting only QButtonGroup's buttonClicked (int) signal to QBitArray's toggleBit (int), ignoring other overloaded signatures, includes indicating the desired type in brackets, like this:

 self.buttonGroup.buttonClicked[int].connect(self.bitArray.toggleBit) 
+16
source

Source: https://habr.com/ru/post/918840/


All Articles