1 import sys
2 from Queue import Queue
3 from threading import currentThread
4
5 from PyQt4 import QtCore, QtGui
6
8 '''
9 A decorator which causes the function to be called by the qt
10 main thread rather than synchronously...
11
12 NOTE: This makes the call async handled by the qt main
13 loop code. you can NOT return anything.
14 '''
15 def idleadd(*args, **kwargs):
16 if currentThread().getName() == 'QtThread':
17 return func(*args, **kwargs)
18
19 return qapp.proxyOneCall(func, *args, **kwargs)
20
21 return idleadd
22
24 '''
25 Similar to idlethread except that it is synchronous and able
26 to return values.
27 '''
28 q = Queue()
29 def dowork(*args, **kwargs):
30 try:
31 q.put(func(*args, **kwargs))
32 except Exception, e:
33 q.put(e)
34
35 def idleadd(*args, **kwargs):
36 if currentThread().getName() == 'QtThread':
37 return func(*args, **kwargs)
38 qapp.proxyOneCall(dowork, *args, **kwargs)
39 return q.get()
40
41 return idleadd
42
44
45 proxyCall = QtCore.pyqtSignal(name='proxyCall')
46
51
53 callable, args, kwargs = self.call_proxy_queue.get()
54 return callable(*args, **kwargs)
55
57 self.call_proxy_queue.put((callable,args,kwargs))
58 self.proxyCall.emit()
59
61 global qapp
62 qapp = VQApplication(sys.argv)
63 if css:
64 qapp.setStyleSheet( css )
65
67 global qapp
68 currentThread().setName('QtThread')
69 sys.exit(qapp.exec_())
70
73