On Tue, Apr 21, 2009 at 2:15 AM, Ori Avtalion <[email protected]> wrote:
> Hi,
>
> I'm trying to create a Qt object that will be exposed to javascript.
> I want that javascript object to be complex: It has nested objects
> inside it, and has arrays with other objects in them.
>
> I'm not sure how to define the properties for the top object that I
> expose. pyqtProperty doesn't accept "QVariant". (It returns the error
> "TypeError: type 'QVariant' is not supported as a property type")
Dynamic properties via setProperty use variants but I think it does
not work correctly for objects and widgets. It seems that the type is
not set to QMetaType::QObjectStar or QMetaType::QWidgetStar in the
constructor. At least QtScript does not recognize the variant as a
object or widget (see app property in attached example - the C++
equivalent would work this way).
> I managed to go around it by defining a function that returns a QVariant:
> [..]
> but I'd really like to have "fun" as a property instead of a function.
Instead of properties you could in some use-cases replace it with
named child objects. (See the timer object in attached example. Use
dir(python) or dir(python.timer) in the shell).
Henning
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtScript import QScriptEngine, QScriptValue
ps1 = ">>> "
ps2 = "... "
app = QApplication(sys.argv)
class Python(QObject):
def __init__(self):
QObject.__init__(self)
self.setObjectName("python")
# Does not work as expected :(
self.setProperty("app", QVariant(qApp))
self.t = QTimer(self)
self.t.setObjectName("timer")
@pyqtSignature("QString")
def hello(self, name):
print "Hello,", name
def get_test(self):
return 123
test = pyqtProperty("int", get_test)
engine = QScriptEngine()
engine.evaluate("function dir(obj) { for(o in obj) print(o); }")
py = Python()
spy = engine.newQObject(py)
engine.globalObject().setProperty("python", spy)
print "Ctrl+D to quit"
prompt = ps1
code = ""
while True:
line = raw_input(prompt)
if not line.strip():
continue
code = code + line + "\n"
if engine.canEvaluate(code):
result = engine.evaluate(code)
if engine.hasUncaughtException():
bt = engine.uncaughtExceptionBacktrace()
print "Traceback:"
print "\n".join([" %s" % l for l in list(bt)])
print engine.uncaughtException().toString()
else:
print result.toString()
code = ""
prompt = ps1
else:
prompt = ps2
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt