1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-02-05 08:08:23 +08:00
pyotherside/examples/qobject_reference.py

66 lines
1.8 KiB
Python
Raw Normal View History

2014-10-09 11:30:18 +02:00
import threading
import time
import pyotherside
2014-10-09 11:30:18 +02:00
# 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)
2014-10-09 11:30:18 +02:00
def do_something(bar):
while True:
print('got: ', bar.dynamicFunction(1, 2, 3))
time.sleep(1)
2014-10-09 11:30:18 +02:00
def foo(bar, py):
# Printing the objects will give some info on the
# QObject class and memory address
print('got:', bar, py)
2014-10-09 11:30:18 +02:00
# 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'))
2014-10-09 11:30:18 +02:00
try:
bar.i_am_pretty_sure_this_attr_does_not_exist = 147
except Exception as e:
print('Got exception (as expected):', e)
2014-10-09 11:30:18 +02:00
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
2014-10-09 11:30:18 +02:00
# Printing a member function gives a bound method
print(bar.dynamicFunction)
2014-10-09 11:30:18 +02:00
# Calling a member function is just as easy
result = bar.dynamicFunction(1, 2, 3)
2014-10-09 11:30:18 +02:00
print('result:', result)
try:
bar.dynamicFunction(1, 2, 3, unexpected=123)
except Exception as e:
print('Got exception (as expected):', e)
2014-10-09 11:30:18 +02:00
threading.Thread(target=do_something, args=[bar]).start()
# Returning QObject references from Python also works
2014-10-09 11:30:18 +02:00
return bar