On Monday 20 December 2010 19:21:04 Vladimír Jícha wrote: > Thanks a lot for your quick answer, Hugo. > > Dne 20.12.2010 16:25, Hugo Parente Lima napsal(a): > > On Monday 20 December 2010 13:32:41 Vladimír Jícha wrote: > >> Hi List, > >> > >> It's me again with some QML related questions. I'm trying to call a > >> function in my Python code from QML UI. I followed the example called > >> "qmltopy2". It works OK. But I wanted more functions with the same input > >> parameters. I thought I only need to write a new function and then call > >> it from QML. But apparently this doesn't work. Please see my modified > >> version of the qmltopy2 example. > >> > >> Is this a bug or is the behavior intended? If it's intended, how to do > >> it right? > > > > val2() *must* be declared as a slot to be visible on QML. > > Thank you, now it works fine. > > >> And talking aout calling Python functions from QML, what about the other > >> way - calling a QML (JavaScript) funcion from Python? I studied the > >> pytoqml1 eample. It shows how to call a function of QML root object. How > >> do I call functions of other objects? > > > > You do it the same way you do to call javascript functions from C++. > > I'm sorry for my ignorance, but unfortunately I have no knowledge of > C++. Could you give me a short code sample, please? Thank you.
The example (a unit test from pyside) is attached, just replace the adjust_filename function by nothing, i.e. just the QML file name. > Vladimir -- Hugo Parente Lima INdT - Instituto Nokia de Tecnologia
''' Test bug 451: http://bugs.openbossa.org/show_bug.cgi?id=451''' from PySide import QtCore, QtGui, QtDeclarative from helper import adjust_filename import sys import unittest class PythonObject(QtCore.QObject): def __init__(self): QtCore.QObject.__init__(self, None) self._called = "" self._arg1 = None self._arg2 = None def setCalled(self, v): self._called = v def setArg1(self, v): self._arg1 = v def setArg2(self, v): self._arg2 = v def getCalled(self): return self._called def getArg1(self): return self._arg1 def getArg2(self): return self._arg2 called = QtCore.Property(str, getCalled, setCalled) arg1 = QtCore.Property(int, getArg1, setArg1) arg2 = QtCore.Property('QVariant', getArg2, setArg2) class TestBug(unittest.TestCase): def testQMLFunctionCall(self): app = QtGui.QApplication(sys.argv) view = QtDeclarative.QDeclarativeView() obj = PythonObject() context = view.rootContext() context.setContextProperty("python", obj) view.setSource(QtCore.QUrl.fromLocalFile(adjust_filename('bug_451.qml', __file__))) root = view.rootObject() root.simpleFunction() self.assertEqual(obj.called, "simpleFunction") root.oneArgFunction(42) self.assertEqual(obj.called, "oneArgFunction") self.assertEqual(obj.arg1, 42) root.twoArgFunction(10, app) self.assertEqual(obj.called, "twoArgFunction") self.assertEqual(obj.arg1, 10) self.assertEqual(obj.arg2, app) rvalue = root.returnFunction() self.assertEqual(obj.called, "returnFunction") self.assertEqual(rvalue, 42) if __name__ == '__main__': unittest.main()
import Qt 4.7
Rectangle {
id: page
function simpleFunction() {
python.called = "simpleFunction"
}
function oneArgFunction(x) {
python.called = "oneArgFunction"
python.arg1 = x
}
function twoArgFunction(x, y) {
python.called = "twoArgFunction"
python.arg1 = x
python.arg2 = y
}
function returnFunction() {
python.called = "returnFunction"
return 42
}
}
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ PySide mailing list [email protected] http://lists.openbossa.org/listinfo/pyside
