I have the following PySide6 code
import sys
from PySide6.QtWidgets import (QApplication,
QWidget, QPushButton, QVBoxLayout, QHBoxLayout,
QLabel)
class Window(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
self.conn_count = 0
h_layout1 = QHBoxLayout()
self.button = QPushButton('Click me!')
self.label = QLabel('Connections: 0')
h_layout1.addWidget(self.button)
h_layout1.addWidget(self.label)
layout.addLayout(h_layout1)
h_layout2 = QHBoxLayout()
connect_button = QPushButton('Connect')
connect_button.clicked.connect(self.on_connect_clicked)
disconnect_button = QPushButton('Disconnect')
disconnect_button.clicked.connect(self.on_disconnect_clicked)
h_layout2.addWidget(connect_button)
h_layout2.addWidget(disconnect_button)
layout.addLayout(h_layout2)
def on_button_clicked(self):
print('Button clicked')
def on_connect_clicked(self):
self.button.clicked.connect(self.on_button_clicked)
self.conn_count += 1
self.label.setText('Connections: ' + str(self.conn_count))
print(self.conn_count)
def on_disconnect_clicked(self):
self.button.clicked.disconnect(self.on_button_clicked)
self.conn_count -= 1
self.label.setText('Connections: ' + str(self.conn_count))
print(self.conn_count)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = Window()
main_window.show()
sys.exit(app.exec())
It connects/disconnects a button clicked signal with a slot named on_button_clicked on 'Connect' and 'Disconnect' buttons click. Here is the output
1
2
3
4
5
6
7
8
9
10
9
8
...001.py:50: RuntimeWarning: Failed to disconnect (<bound method Window.on_button_clicked of <__main__.Window(0x1db021d9430) at 0x000001DB03DAA100>>) from signal "clicked()".
self.button.clicked.disconnect(self.on_button_clicked)
7
6
5
which means the first 'Disconnect' click removes one connection but the second click removes all the rest.
The same script with PyQt6 gives
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
0
which means that it removes connections one by one. Python 3.11.9, PySide 6.9.1, Windows 11.
What is the reason for this PySide6 behavior and is it documented/mentioned anywhere?
Edit:
Reported as a bug here
https://bugreports.qt.io/browse/PYSIDE-3190
as per the maintainer comment "To disconnect single connections, use the Connection ID object returned by connect()."
Edit2: Says it has been fixed and will be merged into PySide 6.10. This is actually very nice and kudos to the team.