File: qobject_reference.py

package info (click to toggle)
pyotherside 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 880 kB
  • sloc: cpp: 2,869; python: 475; makefile: 152; sh: 35
file content (65 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import threading
import time
import pyotherside

# If you try to instantiate a QObject, it's unbound
unbound = pyotherside.QObject()
print(unbound)
try:
    unbound.a = 1
except Exception as e:
    print('Got exception:', e)

def do_something(bar):
    while True:
        print('got: ', bar.dynamicFunction(1, 2, 3))
        time.sleep(1)

def foo(bar, py):
    # Printing the objects will give some info on the
    # QObject class and memory address
    print('got:', bar, py)

    # Ok, this is pretty wicked - we can now call into
    # the PyOtherSide QML element from within Python
    # (not that it's a good idea to do this, mind you..)
    print(py.evaluate)
    print(py.evaluate('3*3'))

    try:
        bar.i_am_pretty_sure_this_attr_does_not_exist = 147
    except Exception as e:
        print('Got exception (as expected):', e)

    try:
        bar.x = 'i dont think i can set this to a string'
    except Exception as e:
        print('Got exception (as expected):', e)

    # This doesn't work yet, because we can't convert a bound
    # member function to a Qt/QML type yet (fallback to None)
    try:
        bar.dynamicFunction(bar.dynamicFunction, 2, 3)
    except Exception as e:
        print('Got exception (as expected):', e)

    # Property access works just like expected
    print(bar.x, bar.color, bar.scale)
    bar.x *= 3

    # Printing a member function gives a bound method
    print(bar.dynamicFunction)

    # Calling a member function is just as easy
    result = bar.dynamicFunction(1, 2, 3)
    print('result:', result)

    try:
        bar.dynamicFunction(1, 2, 3, unexpected=123)
    except Exception as e:
        print('Got exception (as expected):', e)

    threading.Thread(target=do_something, args=[bar]).start()

    # Returning QObject references from Python also works
    return bar