Debian Codegraph @

examples/qobject_reference.py

raw symbols SCIP
1 import threading
2 import time
3 import pyotherside
4
5 # If you try to instantiate a QObject, it's unbound
6 unbound = pyotherside.QObject()
7 print(unbound)
8 try:
9 unbound.a = 1
10 except Exception as e:
11 print('Got exception:', e)
12
13 def do_something(bar):
14 while True:
15 print('got: ', bar.dynamicFunction(1, 2, 3))
16 time.sleep(1)
17
18 def foo(bar, py):
19 # Printing the objects will give some info on the
20 # QObject class and memory address
21 print('got:', bar, py)
22
23 # Ok, this is pretty wicked - we can now call into
24 # the PyOtherSide QML element from within Python
25 # (not that it's a good idea to do this, mind you..)
26 print(py.evaluate)
27 print(py.evaluate('3*3'))
28
29 try:
30 bar.i_am_pretty_sure_this_attr_does_not_exist = 147
31 except Exception as e:
32 print('Got exception (as expected):', e)
33
34 try:
35 bar.x = 'i dont think i can set this to a string'
36 except Exception as e:
37 print('Got exception (as expected):', e)
38
39 # This doesn't work yet, because we can't convert a bound
40 # member function to a Qt/QML type yet (fallback to None)
41 try:
42 bar.dynamicFunction(bar.dynamicFunction, 2, 3)
43 except Exception as e:
44 print('Got exception (as expected):', e)
45
46 # Property access works just like expected
47 print(bar.x, bar.color, bar.scale)
48 bar.x *= 3
49
50 # Printing a member function gives a bound method
51 print(bar.dynamicFunction)
52
53 # Calling a member function is just as easy
54 result = bar.dynamicFunction(1, 2, 3)
55 print('result:', result)
56
57 try:
58 bar.dynamicFunction(1, 2, 3, unexpected=123)
59 except Exception as e:
60 print('Got exception (as expected):', e)
61
62 threading.Thread(target=do_something, args=[bar]).start()
63
64 # Returning QObject references from Python also works
65 return bar