Skip to content

Instantly share code, notes, and snippets.

@sanfx
Created July 4, 2014 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanfx/cc7f1457b2f646a28494 to your computer and use it in GitHub Desktop.
Save sanfx/cc7f1457b2f646a28494 to your computer and use it in GitHub Desktop.
How to emit signal from one class and connect it to SLOT in a different class.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment