Created
July 4, 2014 17:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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