Ciasto piekarz
Ciasto piekarz

Reputation: 8277

Passing exception to slot while emitting signal

I have to pass what exception occured in one function to a slot inside a different class.

_qObject = QtCore.QObject()

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

I want to pass ex to class that has

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

This is my case

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

Upvotes: 1

Views: 784

Answers (2)

Ciasto piekarz
Ciasto piekarz

Reputation: 8277

How Shadow9043 suggested is also right but in comments things got twisted but this is what I found the correct and simplistic approach.

Long Story in Short!!!

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

and in the class where I connect this exception

i will take this ex string as argument

def __errorOccured(self, exStr):
    print exStr

Here is how I have it working, if anyone has got better suggestion in how I achieved it please comment.

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

Upvotes: 0

Shadow9043
Shadow9043

Reputation: 2260

See below code for example:

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

See the new way to connect signals to slots

Upvotes: 2

Related Questions