Advertisement
shushens

Signal-Slot

Feb 5th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. class Gui(QtCore.QObject, object):  # This is class A that is instanced elsewhere
  2.     capture = cv2.cv.CaptureFromCAM(-1) # Capturing webcam image
  3.     mySignal = QtCore.SIGNAL('mySignal(str)')
  4.  
  5.     def __init__(self, name=None, parent_node=None, **kwd):
  6.     QtCore.QObject.__init__(self)
  7.  
  8.     def init_handler(self,  **kwd):  # The initial declarations go here
  9.     self.gui=guiThread(self)
  10.     self.gui.start()
  11.  
  12.     def compute_handler(self):   # This is the loop that will keep executing periodically  
  13.         window = self.gui.window      # This window is the gui created in QtDesigner
  14.     self.tempimage = cv2.cv.QueryFrame(self.capture)
  15.     print "about to emit"
  16.     self.emit(QtCore.SIGNAL('mySignal'),"Hello World")
  17.         print "emitted"
  18.  
  19. class guiThread(Thread, QtCore.QObject, object):
  20.  
  21.     def __init__ (self, creator):
  22.     QtCore.QObject.__init__(self)
  23.     self._creator = creator  # And now self._creator is the creator object
  24.         Thread.__init__(self)
  25.         self.app = None
  26.     self.window = None
  27.  
  28.     def run(self):
  29.     self.app = QtGui.QApplication(sys.argv)
  30.         self.window = MainWindow()
  31.         self.window.show()
  32.     print "about to connect"
  33.     self.connect(self._creator, QtCore.SIGNAL('mySignal'),self.printSuccess)
  34.     print "passed the connect"
  35.     self.app.exec_()
  36.    
  37.     def printSuccess(self, var):
  38.     print "received", var
  39.  
  40.     def stop(self):
  41.         self.window.close()
  42.    
  43.  
  44. ______________ OUTPUT ______________
  45. about to connect
  46. passed the connect
  47. about to emit
  48. emitted
  49. about to emit
  50. emitted
  51. about to emit
  52. emitted
  53. about to emit
  54. emitted
  55. about to emit
  56. emitted
  57. .....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement