发出信号时将异常传递给插槽

时间:2014-06-30 10:49:30

标签: qt pyqt pyside signals-slots

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

_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()

2 个答案:

答案 0 :(得分:2)

参见下面的代码:

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将信号连接到广告位

答案 1 :(得分:0)

Shadow9043的建议也是正确的,但在评论中事情变得扭曲,但这就是我找到的正确和简单的方法。

简短的故事!!!

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_())