Last active
October 1, 2020 05:39
-
-
Save billyshambrook/c695f36a8f10cc375355 to your computer and use it in GitHub Desktop.
Debug QT Signals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import inspect | |
import log | |
from PyQt4 import QtCore | |
from PyQt4 import QtGui | |
logger = log.getLogger(__name__) | |
def log_manual_slot(f): | |
""" | |
Decorate functions that are passed to the signal 'connect' method. | |
Example: | |
>>> mysignal.connect(log_manual_slot(myslot)) | |
""" | |
@functools.wrap(f) | |
def wrapper(*args, **kwargs): | |
signal_name = inspect.stack()[1][0].f_code.co_names[-2] | |
logger.debug('{0}{1} emitted by {2} and picked up by {3}'.format( | |
signal_name, | |
args, | |
str(inspect.stack()[1][3]), | |
f.__name__)) | |
return f(*args) | |
return wrapper | |
def log_automatic_slot(f): | |
""" Decorate 'pyqtSlot' decorated methods. """ | |
@functools.wrap(f) | |
def wrapper(*args, **kwargs): | |
_, signal_name = f.__name__.rsplit('_', 1) | |
logger.debug('{0}{1} emitted by {2} and picked up by {3}'.format( | |
signal_name, | |
args[1:], | |
str(args[0].sender()), | |
f.__name__)) | |
return f(*args) | |
return wrapper | |
class MainWindow(QtGui.QMainWindow): | |
updated = QtCore.pyqtSignal(str) | |
def __init__(self, parent=None): | |
super(MainWindow, self).__init__(parent) | |
self.button = QtGui.QPushButton('My button', self) | |
self.button.setObjectName('mybutton') | |
QtCore.QMetaObject.connectSlotsByName(self) | |
@log_automatic_slot | |
@QtCore.pyqtSlot() | |
def on_mybutton_clicked(self): | |
self.updated.emit('hello, world!') | |
def update_recv(data): | |
1 + 1 | |
def update_recv2(data): | |
1 + 1 | |
if __name__ == '__main__': | |
import sys | |
app = QtGui.QApplication([]) | |
view = MainWindow() | |
view.updated.connect(log_manual_slot(update_recv)) | |
view.updated.connect(log_manual_slot(update_recv2)) | |
view.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment