PDA

View Full Version : PyQt5 callback of different name being invoked on valueChanged



DDR
30th August 2018, 02:09
Hello, everyone! I'm having some trouble with callback in my PyQt5 app.

I want more than one callback available to an object, but defining any subsequent callback in an object overwrites all previous callbacks. However, the callbacks are decorated, which seems to be messing with things. How should I define object-level callbacks, so I can use self in them?

In the following example, which you can run on any computer with Python 3.x and PyQt5.11.x, a spinbox is hooked up to two valueChanged callbacks. These are called A and C. However, the second callback C never triggers, and a third callback D triggers instead. D should not trigger.



from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSlot


def silenceCallbacks(*elements):
"""Silence events for the duration of a callback. Mostly skipped for this reproduction."""
def silenceCallbacksOf(callback):
def silencedCallback(self, *args, **kwargs):
callback(self, *args, **kwargs)
return silencedCallback
return silenceCallbacksOf


@pyqtSlot(int)
@silenceCallbacks()
def callbackA(px: int):
#correctly called
print('got A', px)

@pyqtSlot(int) #this overwrites the last three functions
@silenceCallbacks()
def callbackB(px: int):
#correctly not called
print('got B', px)


class RecordingSettings(QtWidgets.QDialog):
@pyqtSlot(int)
@silenceCallbacks()
def callbackC(self, px: int):
#incorrectly not called
print('got C', px)

@pyqtSlot(int) #this overwrites the previous pyqtSlot-decorated function
@silenceCallbacks()
def callbackD(self, px: int):
#incorrectly called
print('got D', px)


def __init__(self, window):
super().__init__()

spin = QtWidgets.QSpinBox()

spin.valueChanged.connect(callbackA)
spin.valueChanged.connect(self.callbackC)

layout = QtWidgets.QVBoxLayout()
layout.addWidget(spin)
self.setLayout(layout)
self.show()



app = QtWidgets.QApplication([])
recSettingsWindow = RecordingSettings(app)
recSettingsWindow.show()
app.exec_()


If anyone's got any ideas, that'd be much appreciated. Thank you. :crying: