2

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.

6
  • 1
    I can reproduce this also on Linux with PySide 6.8.0. It seems related to changes done to PySide (see PYSIDE-1275 and PYSIDE-2705); I cannot test with earlier PySide6 versions, but this doesn't happen with PySide2, so I'm inclined to think it's due to the above. You should consider reporting it (possibly referencing those two). Out of curiosity, while completely "legal", it's usually uncommon to connect the same signal/slot pairs more than once, why do you need to do that? Commented Sep 1 at 12:09
  • I don't need to, I am just testing the mechanism. Commented Sep 1 at 12:12
  • Nonetheless, I still suggest you to file a report on bugreports.qt.io and click the "Answer your question" button with a link to that report, then ensure that you update its contents whenever the bug receives any relevant change. Commented Sep 2 at 1:40
  • @musicamante Sure I'll do that in a few days, just to see if someone else has any insights on this. Commented Sep 2 at 15:48
  • 1
    Please don't edit the question to report that this is now fixed, use the "Answer your question" button and explain it there, including any relevant aspect that may be affect previous and future versions, or possible workarounds if necessary (as indicated in the comments to the report). Commented Sep 28 at 18:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.