This amazing answer of jlujan shows how to add exception handling to a pyqtslot.
Preventing PyQt to silence exceptions occurring in slots
import sys
import traceback
import types
import functools
import PyQt5.QtCore
import PyQt5.QtQuick
import PyQt5.QtQml
def MyPyQtSlot(*args):
if len(args) == 0 or isinstance(args[0], types.FunctionType):
args = []
@PyQt5.QtCore.pyqtSlot(*args)
def slotdecorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args)
except:
print("Uncaught Exception in slot")
traceback.print_exc()
return wrapper
return slotdecorator
class MyClass(PyQt5.QtQuick.QQuickView):
def __init__(self, qml_file='myui.qml'):
super().__init__(parent)
self.setSource(QUrl.fromLocalFile(qml_file))
self.rootContext().setContextProperty('myObject', self)
@MyPyQtSlot()
def buttonClicked(self):
print("clicked")
raise Exception("wow")
But when I try to call this slot from QML, it gives me the following error
Button {
id: btnTestException
height: 80
width: 200
text: "Do stuff"
onClicked: {
myObject.mySlot();
}
}
file:myui.qml:404: TypeError: Property 'mySlot' of object MyClass(0xff30c20) is not a function
How do I fix this?