【问题标题】:Passing exception to slot while emitting signal发出信号时将异常传递给插槽
【发布时间】:2014-08-20 17:21:03
【问题描述】:

我必须将一个函数中发生的异常传递给另一个类中的插槽。

_qObject = QtCore.QObject()

except Exception, ex:
    QtCore.QObject.emit(_qObject, QtCore.SIGNAL("error_occured"))

我想将 ex 传递给具有

的类
QtCore.QObject.connect(_qObject, QtCore.SIGNAL("error_occured"), self.__errorOccured)

这是我的情况

from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtGui import QWidget, QApplication

has_error = pyqtSignal(Exception)

class SomeOtherClass(QWidget):

    # this is my UI class
    def __init__(self, parent=None):
        super(SomeOtherClass, self).__init__(parent)

        # Initialise the Class and connect signal to slot
        has_error.connect(self.thrown_error)


    @pyqtSlot(Exception)
    def thrown_error(self, my_err):
        #Do Stuff with the Exception
        print(type(my_err), my_err)
        self.close()


def makeError():
    try:
        print 1/0
    except ZeroDivisionError, ze:
        has_error.emit(ze)

app = QApplication([])
SomeOtherClass()

【问题讨论】:

    标签: qt pyqt pyside signals-slots


    【解决方案1】:

    例如看下面的代码:

    from PyQt4.QtCore import pyqtSignal, pyqtSlot
    from PyQt4.QtGui import QWidget, QApplication
    import sys    
    
    class SomeClass(QWidget):
        # Declare a new signal - passes Exception
        has_error = pyqtSignal(Exception)
    
        def __init__(self, parent=None):
            super(SomeClass, self).__init__(parent)
    
        def run_something(self):
            #Force an Error
            try:
                1 / 0
            except ZeroDivisionError as ze:
                #Emit the Signal
                self.has_error.emit(ze)
    
    
    class SomeOtherClass(QWidget):
        def __init__(self, parent=None):
            super(SomeOtherClass, self).__init__(parent)
    
            # Initialise the Class and connect signal to slot
            class1 = SomeClass()
            class1.has_error.connect(self.thrown_error)
            class1.run_something()
    
        @pyqtSlot(Exception)
        def thrown_error(self, my_err):
            #Do Stuff with the Exception
            print(type(my_err), my_err)
    
    
    app = QApplication(sys.argv)
    SomeOtherClass()
    

    请参阅the new way 将信号连接到插槽

    【讨论】:

    • @pyqtSlot(Exception)thrown_error(...) 方法之上做了什么
    • @pyqtSlot() 是装饰器,Exception 是类型。它将这个方法声明为一个槽,这样 Qt 就不会绑定它两次。请参阅我在示例底部链接的文档。
    • 我也没有SomeClass(),它只是模块级别的功能。
    • 在这种情况下,您不需要信号/插槽机制。只需在您的try...except 块中调用SomeClass.some_method(my_err)
    • 或者在你的函数周围加上一个class 并使用上面的例子。
    【解决方案2】:

    Shadow9043 的建议也是正确的,但在 cmets 中事情变得扭曲了,但这是我发现的正确且简单的方法。

    长话短说!!!

    except Exception, ex:
        QtCore.QObject.emit(_qObject, QtCore.SIGNAL("error_occured"), str(ex))
    

    在我连接这个异常的类中

    我将把这个 ex 字符串作为参数

    def __errorOccured(self, exStr):
        print exStr
    

    这是我的工作方式,如果有人对我如何实现它有更好的建议,请发表评论。

    from PyQt4.QtCore import pyqtSignal, pyqtSlot
    from PyQt4.QtGui import QWidget, QApplication
    from PyQt4 import QtCore
    import sys
    
    _qObject = QtCore.QObject()
    
    
    class SomeOtherClass(QWidget):
    
        # this is my UI class
    
        def __init__(self, parent=None):
            super(SomeOtherClass, self).__init__(parent)
    
            # Initialise the Class and connect signal to slot
            QtCore.QObject.connect(_qObject, QtCore.SIGNAL("error_occured"), self.thrown_error)
    
    
        def thrown_error(self, my_err):
            #Do Stuff with the Exception
            print(type(my_err), my_err)
    
    
    def makeError():
        try:
            print 1/0
        except ZeroDivisionError, ex:
            QtCore.QObject.emit(_qObject, QtCore.SIGNAL("error_occured"), str(ex))
    
    app = QApplication(sys.argv)
    win = SomeOtherClass()
    makeError()
    win.show()
    sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 2011-07-06
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2019-04-25
      相关资源
      最近更新 更多