1

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?

Community
  • 1
  • 1
Pieter
  • 1,191
  • 1
  • 11
  • 29
  • 1
    Upgrade to PyQt-5.5 or greater. The decorator in your example has been made redundant by [changes to the way pyqt deals with unhandled exceptions](http://pyqt.sourceforge.net/Docs/PyQt5/incompatibilities.html#pyqt-v5-5). – ekhumoro Jul 08 '16 at 00:33

0 Answers0