我如何装饰pyqtslot装饰器?

时间:2016-07-07 09:39:32

标签: python python-3.x pyqt qml pyqt5

这个jlujan的惊人答案显示了如何向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")

但是当我尝试从QML调用此插槽时,它会给我以下错误

    Button {
        id: btnTestException
        height: 80
        width: 200
        text: "Do stuff"
        onClicked: {
            myObject.mySlot();
        }
    }
  

file:myui.qml:404:TypeError:对象的属性'mySlot'   MyClass(0xff30c20)不是函数

如何解决此问题?

0 个答案:

没有答案