source: trunk/doc/src/snippets/code/doc_src_qtscript.qdoc@ 728

Last change on this file since 728 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 23.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//! [0]
43#include <QtScript>
44//! [0]
45
46
47//! [1]
48QT += script
49//! [1]
50
51
52//! [2]
53function myInterestingScriptFunction() { ... }
54...
55myQObject.somethingChanged.connect(myInterestingScriptFunction);
56//! [2]
57
58
59//! [3]
60myQObject.somethingChanged.connect(myOtherQObject.doSomething);
61//! [3]
62
63
64//! [4]
65myQObject.somethingChanged.disconnect(myInterestingFunction);
66myQObject.somethingChanged.disconnect(myOtherQObject.doSomething);
67//! [4]
68
69
70//! [5]
71var obj = { x: 123 };
72var fun = function() { print(this.x); };
73myQObject.somethingChanged.connect(obj, fun);
74//! [5]
75
76
77//! [6]
78myQObject.somethingChanged.disconnect(obj, fun);
79//! [6]
80
81
82//! [7]
83var obj = { x: 123, fun: function() { print(this.x); } };
84myQObject.somethingChanged.connect(obj, "fun");
85//! [7]
86
87
88//! [8]
89myQObject.somethingChanged.disconnect(obj, "fun");
90//! [8]
91
92
93//! [9]
94try {
95 myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist");
96} catch (e) {
97 print(e);
98}
99//! [9]
100
101
102//! [10]
103myQObject.somethingChanged("hello");
104//! [10]
105
106
107//! [11]
108myQObject.myOverloadedSlot(10); // will call the int overload
109myQObject.myOverloadedSlot("10"); // will call the QString overload
110//! [11]
111
112
113//! [12]
114myQObject['myOverloadedSlot(int)']("10"); // call int overload; the argument is converted to an int
115myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string
116//! [12]
117
118
119//! [13]
120Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
121//! [13]
122
123
124//! [14]
125myQObject.enabled = true;
126
127...
128
129myQObject.enabled = !myQObject.enabled;
130//! [14]
131
132
133//! [15]
134myDialog.okButton
135//! [15]
136
137
138//! [16]
139myDialog.okButton.objectName = "cancelButton";
140// from now on, myDialog.cancelButton references the button
141//! [16]
142
143
144//! [17]
145var okButton = myDialog.findChild("okButton");
146if (okButton != null) {
147 // do something with the OK button
148}
149
150var buttons = myDialog.findChildren(RegExp("button[0-9]+"));
151for (var i = 0; i < buttons.length; ++i) {
152 // do something with buttons[i]
153}
154//! [17]
155
156
157//! [18]
158QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
159{
160 // let the engine manage the new object's lifetime.
161 return engine->newQObject(new MyQObject(), QScriptEngine::ScriptOwnership);
162}
163//! [18]
164
165
166//! [19]
167class MyObject : public QObject
168{
169 Q_OBJECT
170
171public:
172 MyObject( ... );
173
174 void aNonScriptableFunction();
175
176public slots: // these functions (slots) will be available in QtScript
177 void calculate( ... );
178 void setEnabled( bool enabled );
179 bool isEnabled() const;
180
181private:
182 ....
183
184};
185//! [19]
186
187
188//! [20]
189class MyObject : public QObject
190{
191 Q_OBJECT
192
193 public:
194 Q_INVOKABLE void thisMethodIsInvokableInQtScript();
195 void thisMethodIsNotInvokableInQtScript();
196
197 ...
198};
199//! [20]
200
201
202//! [21]
203var obj = new MyObject;
204obj.setEnabled( true );
205print( "obj is enabled: " + obj.isEnabled() );
206//! [21]
207
208
209//! [22]
210var obj = new MyObject;
211obj.enabled = true;
212print( "obj is enabled: " + obj.enabled );
213//! [22]
214
215
216//! [23]
217class MyObject : public QObject
218{
219 Q_OBJECT
220 // define the enabled property
221 Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled )
222
223public:
224 MyObject( ... );
225
226 void aNonScriptableFunction();
227
228public slots: // these functions (slots) will be available in QtScript
229 void calculate( ... );
230 void setEnabled( bool enabled );
231 bool isEnabled() const;
232
233private:
234 ....
235
236};
237//! [23]
238
239
240//! [24]
241Q_PROPERTY(int nonScriptableProperty READ foo WRITE bar SCRIPTABLE false)
242//! [24]
243
244
245//! [25]
246class MyObject : public QObject
247{
248 Q_OBJECT
249 // define the enabled property
250 Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled )
251
252public:
253 MyObject( ... );
254
255 void aNonScriptableFunction();
256
257public slots: // these functions (slots) will be available in QtScript
258 void calculate( ... );
259 void setEnabled( bool enabled );
260 bool isEnabled() const;
261
262signals: // the signals
263 void enabledChanged( bool newState );
264
265private:
266 ....
267
268};
269//! [25]
270
271
272//! [26]
273function enabledChangedHandler( b )
274{
275 print( "state changed to: " + b );
276}
277
278function init()
279{
280 var obj = new MyObject();
281 // connect a script function to the signal
282 obj["enabledChanged(bool)"].connect(enabledChangedHandler);
283 obj.enabled = true;
284 print( "obj is enabled: " + obj.enabled );
285}
286//! [26]
287
288
289//! [27]
290var o = new Object();
291o.foo = 123;
292print(o.hasOwnProperty('foo')); // true
293print(o.hasOwnProperty('bar')); // false
294print(o); // calls o.toString(), which returns "[object Object]"
295//! [27]
296
297
298//! [28]
299function Person(name)
300{
301 this.name = name;
302}
303//! [28]
304
305
306//! [29]
307Person.prototype.toString = function() { return "Person(name: " + this.name + ")"; }
308//! [29]
309
310
311//! [30]
312var p1 = new Person("John Doe");
313var p2 = new Person("G.I. Jane");
314print(p1); // "Person(name: John Doe)"
315print(p2); // "Person(name: G.I. Jane)"
316//! [30]
317
318
319//! [31]
320print(p1.hasOwnProperty('name')); // 'name' is an instance variable, so this returns true
321print(p1.hasOwnProperty('toString')); // returns false; inherited from prototype
322print(p1 instanceof Person); // true
323print(p1 instanceof Object); // true
324//! [31]
325
326
327//! [32]
328function Employee(name, salary)
329{
330 Person.call(this, name); // call base constructor
331
332 this.salary = salary;
333}
334
335// set the prototype to be an instance of the base class
336Employee.prototype = new Person();
337
338// initialize prototype
339Employee.prototype.toString = function() { ... }
340//! [32]
341
342
343//! [33]
344var e = new Employee("Johnny Bravo", 5000000);
345print(e instanceof Employee); // true
346print(e instanceof Person); // true
347print(e instanceof Object); // true
348print(e instanceof Array); // false
349//! [33]
350
351
352//! [34]
353QScriptValue Person_ctor(QScriptContext *context, QScriptEngine *engine)
354{
355 QString name = context->argument(0).toString();
356 context->thisObject().setProperty("name", name);
357 return engine->undefinedValue();
358}
359//! [34]
360
361
362//! [35]
363QScriptValue Person_prototype_toString(QScriptContext *context, QScriptEngine *engine)
364{
365 QString name = context->thisObject().property("name").toString();
366 QString result = QString::fromLatin1("Person(name: %0)").arg(name);
367 return result;
368}
369//! [35]
370
371
372//! [36]
373QScriptEngine engine;
374QScriptValue ctor = engine.newFunction(Person_ctor);
375ctor.property("prototype").setProperty("toString", engine.newFunction(Person_prototype_toString));
376QScriptValue global = engine.globalObject();
377global.setProperty("Person", ctor);
378//! [36]
379
380
381//! [37]
382QScriptValue Employee_ctor(QScriptContext *context, QScriptEngine *engine)
383{
384 QScriptValue super = context->callee().property("prototype").property("constructor");
385 super.call(context->thisObject(), QScriptValueList() << context->argument(0));
386 context->thisObject().setProperty("salary", context->argument(1));
387 return engine->undefinedValue();
388}
389//! [37]
390
391
392//! [38]
393QScriptValue empCtor = engine.newFunction(Employee_ctor);
394empCtor.setProperty("prototype", global.property("Person").construct());
395global.setProperty("Employee", empCtor);
396//! [38]
397
398
399//! [39]
400Q_DECLARE_METATYPE(QPointF)
401Q_DECLARE_METATYPE(QPointF*)
402
403QScriptValue QPointF_prototype_x(QScriptContext *context, QScriptEngine *engine)
404{
405 // Since the point is not to be modified, it's OK to cast to a value here
406 QPointF point = qscriptvalue_cast<QPointF>(context->thisObject());
407 return point.x();
408}
409
410QScriptValue QPointF_prototype_setX(QScriptContext *context, QScriptEngine *engine)
411{
412 // Cast to a pointer to be able to modify the underlying C++ value
413 QPointF *point = qscriptvalue_cast<QPointF*>(context->thisObject());
414 if (!point)
415 return context->throwError(QScriptContext::TypeError, "QPointF.prototype.setX: this object is not a QPointF");
416 point->setX(context->argument(0).toNumber());
417 return engine->undefinedValue();
418}
419//! [39]
420
421
422//! [40]
423var o = new Object();
424(o.__proto__ === Object.prototype); // this evaluates to true
425//! [40]
426
427
428//! [41]
429var o = new Object();
430o.__defineGetter__("x", function() { return 123; });
431var y = o.x; // 123
432//! [41]
433
434
435//! [42]
436var o = new Object();
437o.__defineSetter__("x", function(v) { print("and the value is:", v); });
438o.x = 123; // will print "and the value is: 123"
439//! [42]
440
441
442//! [43]
443class MyObject : public QObject
444{
445 Q_OBJECT
446 ...
447};
448
449Q_DECLARE_METATYPE(MyObject*)
450
451QScriptValue myObjectToScriptValue(QScriptEngine *engine, MyObject* const &in)
452{ return engine->newQObject(in); }
453
454void myObjectFromScriptValue(const QScriptValue &object, MyObject* &out)
455{ out = qobject_cast<MyObject*>(object.toQObject()); }
456
457...
458
459qScriptRegisterMetaType(&engine, myObjectToScriptValue, myObjectFromScriptValue);
460//! [43]
461
462//! [44]
463QScriptValue QPoint_ctor(QScriptContext *context, QScriptEngine *engine)
464{
465 int x = context->argument(0).toInt32();
466 int y = context->argument(1).toInt32();
467 return engine->toScriptValue(QPoint(x, y));
468}
469
470...
471
472engine.globalObject().setProperty("QPoint", engine.newFunction(QPoint_ctor));
473//! [44]
474
475//! [45]
476QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
477{
478 QString result;
479 for (int i = 0; i < context->argumentCount(); ++i) {
480 if (i > 0)
481 result.append(" ");
482 result.append(context->argument(i).toString());
483 }
484
485 QScriptValue calleeData = context->callee().data();
486 QPlainTextEdit *edit = qobject_cast<QPlainTextEdit*>(calleeData.toQObject());
487 edit->appendPlainText(result);
488
489 return engine->undefinedValue();
490}
491//! [45]
492
493//! [46]
494int main(int argc, char **argv)
495{
496 QApplication app(argc, argv);
497
498 QScriptEngine eng;
499 QPlainTextEdit edit;
500
501 QScriptValue fun = eng.newFunction(myPrintFunction);
502 fun.setData(eng.newQObject(&edit));
503 eng.globalObject().setProperty("print", fun);
504
505 eng.evaluate("print('hello', 'world')");
506
507 edit.show();
508 return app.exec();
509}
510//! [46]
511
512
513//! [47]
514QScriptEngine eng;
515QLineEdit *edit = new QLineEdit(...);
516QScriptValue handler = eng.evaluate("(function(text) { print('text was changed to', text); })");
517qScriptConnect(edit, SIGNAL(textChanged(const QString &)), QScriptValue(), handler);
518//! [47]
519
520//! [48]
521QLineEdit *edit1 = new QLineEdit(...);
522QLineEdit *edit2 = new QLineEdit(...);
523
524QScriptValue handler = eng.evaluate("(function() { print('I am', this.name); })");
525QScriptValue obj1 = eng.newObject();
526obj1.setProperty("name", "the walrus");
527QScriptValue obj2 = eng.newObject();
528obj2.setProperty("name", "Sam");
529
530qScriptConnect(edit1, SIGNAL(returnPressed()), obj1, handler);
531qScriptConnect(edit2, SIGNAL(returnPressed()), obj2, handler);
532//! [48]
533
534//! [49]
535var getProperty = function(name) { return this[name]; };
536
537name = "Global Object"; // creates a global variable
538print(getProperty("name")); // "Global Object"
539
540var myObject = { name: 'My Object' };
541print(getProperty.call(myObject, "name")); // "My Object"
542
543myObject.getProperty = getProperty;
544print(myObject.getProperty("name")); // "My Object"
545
546getProperty.name = "The getProperty() function";
547getProperty.getProperty = getProperty;
548getProperty.getProperty("name"); // "The getProperty() function"
549//! [49]
550
551//! [50]
552var o = { a: 1, b: 2, sum: function() { return a + b; } };
553print(o.sum()); // reference error, or sum of global variables a and b!!
554//! [50]
555
556//! [51]
557var o = { a: 1, b: 2, sum: function() { return this.a + this.b; } };
558print(o.sum()); // 3
559//! [51]
560
561//! [52]
562QScriptValue getProperty(QScriptContext *ctx, QScriptEngine *eng)
563{
564 QString name = ctx->argument(0).toString();
565 return ctx->thisObject().property(name);
566}
567//! [52]
568
569//! [53]
570QScriptValue myCompare(QScriptContext *ctx, QScriptEngine *eng)
571{
572 double first = ctx->argument(0).toNumber();
573 double second = ctx->argument(1).toNumber();
574 int result;
575 if (first == second)
576 result = 0;
577 else if (first < second)
578 result = -1;
579 else
580 result = 1;
581 return result;
582}
583//! [53]
584
585//! [54]
586QScriptEngine eng;
587QScriptValue comparefn = eng.newFunction(myCompare);
588QScriptValue array = eng.evaluate("new Array(10, 5, 20, 15, 30)");
589array.property("sort").call(array, QScriptValueList() << comparefn);
590
591// prints "5,10,15,20,30"
592qDebug() << array.toString();
593//! [54]
594
595//! [55]
596QScriptValue rectifier(QScriptContext *ctx, QScriptEngine *eng)
597{
598 QRectF magicRect = qscriptvalue_cast<QRectF>(ctx->callee().data());
599 QRectF sourceRect = qscriptvalue_cast<QRectF>(ctx->argument(0));
600 return eng->toScriptValue(sourceRect.intersected(magicRect));
601}
602
603...
604
605QScriptValue fun = eng.newFunction(rectifier);
606QRectF magicRect = QRectF(10, 20, 30, 40);
607fun.setData(eng.toScriptValue(magicRect));
608eng.globalObject().setProperty("rectifier", fun);
609//! [55]
610
611//! [56]
612function add(a, b) {
613 return a + b;
614}
615//! [56]
616
617//! [57]
618function add() {
619 return arguments[0] + arguments[1];
620}
621//! [57]
622
623//! [58]
624QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
625{
626 double a = ctx->argument(0).toNumber();
627 double b = ctx->argument(1).toNumber();
628 return a + b;
629}
630//! [58]
631
632//! [59]
633function add() {
634 if (arguments.length != 2)
635 throw Error("add() takes exactly two arguments");
636 return arguments[0] + arguments[1];
637}
638//! [59]
639
640//! [60]
641function add() {
642 if (arguments.length != 2)
643 throw Error("add() takes exactly two arguments");
644 if (typeof arguments[0] != "number")
645 throw TypeError("add(): first argument is not a number");
646 if (typeof arguments[1] != "number")
647 throw TypeError("add(): second argument is not a number");
648 return arguments[0] + arguments[1];
649}
650//! [60]
651
652//! [61]
653function add() {
654 if (arguments.length != 2)
655 throw Error("add() takes exactly two arguments");
656 return Number(arguments[0]) + Number(arguments[1]);
657}
658//! [61]
659
660//! [62]
661QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
662{
663 if (ctx->argumentCount() != 2)
664 return ctx->throwError("add() takes exactly two arguments");
665 double a = ctx->argument(0).toNumber();
666 double b = ctx->argument(1).toNumber();
667 return a + b;
668}
669//! [62]
670
671//! [63]
672QScriptValue add(QScriptContext *ctx, QScriptEngine *eng)
673{
674 if (ctx->argumentCount() != 2)
675 return ctx->throwError("add() takes exactly two arguments");
676 if (!ctx->argument(0).isNumber())
677 return ctx->throwError(QScriptContext::TypeError, "add(): first argument is not a number");
678 if (!ctx->argument(1).isNumber())
679 return ctx->throwError(QScriptContext::TypeError, "add(): second argument is not a number");
680 double a = ctx->argument(0).toNumber();
681 double b = ctx->argument(1).toNumber();
682 return a + b;
683}
684//! [63]
685
686//! [64]
687function concat() {
688 var result = "";
689 for (var i = 0; i < arguments.length; ++i)
690 result += String(arguments[i]);
691 return result;
692}
693//! [64]
694
695//! [65]
696QScriptValue concat(QScriptContext *ctx, QScriptEngine *eng)
697{
698 QString result = "";
699 for (int i = 0; i < ctx->argumentCount(); ++i)
700 result += ctx->argument(i).toString();
701 return result;
702}
703//! [65]
704
705//! [66]
706function sort(comparefn) {
707 if (comparefn == undefined)
708 comparefn = /* the built-in comparison function */;
709 else if (typeof comparefn != "function")
710 throw TypeError("sort(): argument must be a function");
711 ...
712}
713//! [66]
714
715//! [67]
716QScriptValue sort(QScriptContext *ctx, QScriptEngine *eng)
717{
718 QScriptValue comparefn = ctx->argument(0);
719 if (comparefn.isUndefined())
720 comparefn = /* the built-in comparison function */;
721 else if (!comparefn.isFunction())
722 return ctx->throwError(QScriptContext::TypeError, "sort(): argument is not a function");
723 ...
724}
725//! [67]
726
727//! [68]
728function foo() {
729 // Let bar() take care of this.
730 print("calling bar() with " + arguments.length + "arguments");
731 var result = return bar.apply(this, arguments);
732 print("bar() returned" + result);
733 return result;
734}
735//! [68]
736
737//! [69]
738QScriptValue foo(QScriptContext *ctx, QScriptEngine *eng)
739{
740 QScriptValue bar = eng->globalObject().property("bar");
741 QScriptValue arguments = ctx->argumentsObject();
742 qDebug() << "calling bar() with" << arguments.property("length").toInt32() << "arguments";
743 QScriptValue result = bar.apply(ctx->thisObject(), arguments);
744 qDebug() << "bar() returned" << result.toString();
745 return result;
746}
747//! [69]
748
749//! [70]
750function counter() {
751 var count = 0;
752 return function() {
753 return count++;
754 }
755}
756//! [70]
757
758//! [71]
759var c1 = counter(); // create a new counter function
760var c2 = counter(); // create a new counter function
761print(c1()); // 0
762print(c1()); // 1
763print(c2()); // 0
764print(c2()); // 1
765//! [71]
766
767//! [72]
768QScriptValue counter(QScriptContext *ctx, QScriptEngine *eng)
769{
770 QScriptValue act = ctx->activationObject();
771 act.setProperty("count", 0);
772 QScriptValue result = eng->newFunction(counter_inner);
773 result.setScope(act);
774 return result;
775}
776//! [72]
777
778//! [73]
779QScriptValue counter_inner(QScriptContext *ctx, QScriptEngine *eng)
780{
781 QScriptValue outerAct = ctx->callee().scope();
782 double count = outerAct.property("count").toNumber();
783 outerAct.setProperty("count", count+1);
784 return count;
785}
786//! [73]
787
788//! [74]
789QScriptValue counter_hybrid(QScriptContext *ctx, QScriptEngine *eng)
790{
791 QScriptValue act = ctx->activationObject();
792 act.setProperty("count", 0);
793 return eng->evaluate("(function() { return count++; })");
794}
795//! [74]
796
797//! [75]
798function Book(isbn) {
799 this.isbn = isbn;
800}
801
802var coolBook1 = new Book("978-0131872493");
803var coolBook2 = new Book("978-1593271473");
804//! [75]
805
806//! [76]
807QScriptValue Person_ctor(QScriptContext *ctx, QScriptEngine *eng)
808{
809 QScriptValue object;
810 if (ctx->isCalledAsConstructor()) {
811 object = ctx->thisObject();
812 } else {
813 object = eng->newObject();
814 object.setPrototype(ctx->callee().property("prototype"));
815 }
816 object.setProperty("name", ctx->argument(0));
817 return object;
818}
819//! [76]
820
821//! [77]
822QScriptContext *ctx = eng.pushContext();
823QScriptValue act = ctx->activationObject();
824act.setProperty("digit", 7);
825
826qDebug() << eng.evaluate("digit + 1").toNumber(); // 8
827
828eng.popContext();
829//! [77]
830
831//! [78]
832QScriptValue getSet(QScriptContext *ctx, QScriptEngine *eng)
833{
834 QScriptValue obj = ctx->thisObject();
835 QScriptValue data = obj.data();
836 if (!data.isValid()) {
837 data = eng->newObject();
838 obj.setData(data);
839 }
840 QScriptValue result;
841 if (ctx->argumentCount() == 1) {
842 QString str = ctx->argument(0).toString();
843 str.replace("Roberta", "Ken");
844 result = str;
845 data.setProperty("x", result);
846 } else {
847 result = data.property("x");
848 }
849 return result;
850}
851//! [78]
852
853//! [79]
854QScriptEngine eng;
855QScriptValue obj = eng.newObject();
856obj.setProperty("x", eng.newFunction(getSet),
857 QScriptValue::PropertyGetter|QScriptValue::PropertySetter);
858//! [79]
859
860//! [80]
861obj.x = "Roberta sent me";
862print(obj.x); // "Ken sent me"
863obj.x = "I sent the bill to Roberta";
864print(obj.x); // "I sent the bill to Ken"
865//! [80]
866
867//! [81]
868obj = {};
869obj.__defineGetter__("x", function() { return this._x; });
870obj.__defineSetter__("x", function(v) { print("setting x to", v); this._x = v; });
871obj.x = 123;
872//! [81]
873
874//! [82]
875myButton.text = qsTr("Hello world!");
876//! [82]
877
878//! [83]
879myButton.text = qsTranslate("MyAwesomeScript", "Hello world!");
880//! [83]
881
882//! [84]
883FriendlyConversation.prototype.greeting = function(type)
884{
885 if (FriendlyConversation['greeting_strings'] == undefined) {
886 FriendlyConversation['greeting_strings'] = [
887 QT_TR_NOOP("Hello"),
888 QT_TR_NOOP("Goodbye")
889 ];
890 }
891 return qsTr(FriendlyConversation.greeting_strings[type]);
892}
893//! [84]
894
895//! [85]
896FriendlyConversation.prototype.greeting = function(type)
897{
898 if (FriendlyConversation['greeting_strings'] == undefined) {
899 FriendlyConversation['greeting_strings'] = [
900 QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
901 QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
902 ];
903 }
904 return qsTranslate("FriendlyConversation", FriendlyConversation.greeting_strings[type]);
905}
906//! [85]
907
908//! [86]
909FileCopier.prototype.showProgress = function(done, total, currentFileName)
910{
911 this.label.text = qsTr("%1 of %2 files copied.\nCopying: %3")
912 .arg(done)
913 .arg(total)
914 .arg(currentFileName));
915}
916//! [86]
917
918//! [87]
919lupdate myscript.qs -ts myscript_la.ts
920//! [87]
921
922//! [88]
923lupdate -extensions qs scripts/ -ts scripts_la.ts
924//! [88]
925
926//! [89]
927lrelease myscript_la.ts
928//! [89]
929
930//! [90]
931({ unitName: "Celsius",
932 toKelvin: function(x) { return x + 273; }
933 })
934//! [90]
935
936//! [91]
937QScriptValue object = engine.evaluate("({ unitName: 'Celsius', toKelvin: function(x) { return x + 273; } })");
938QScriptValue toKelvin = object.property("toKelvin");
939QScriptValue result = toKelvin.call(object, QScriptValueList() << 100);
940qDebug() << result.toNumber(); // 373
941//! [91]
942
943//! [92]
944QScriptValue add = engine.globalObject().property("add");
945qDebug() << add.call(QScriptValue(), QScriptValueList() << 1 << 2).toNumber(); // 3
946//! [92]
947
948//! [93]
949typedef QSharedPointer<QXmlStreamReader> XmlStreamReaderPointer;
950
951Q_DECLARE_METATYPE(XmlStreamReaderPointer)
952
953QScriptValue constructXmlStreamReader(QScriptContext *context, QScriptEngine *engine)
954{
955 if (!context->isCalledAsConstructor())
956 return context->throwError(QScriptContext::SyntaxError, "please use the 'new' operator");
957
958 QIODevice *device = qobject_cast<QIODevice*>(context->argument(0).toQObject());
959 if (!device)
960 return context->throwError(QScriptContext::TypeError, "please supply a QIODevice as first argument");
961
962 // Create the C++ object
963 QXmlStreamReader *reader = new QXmlStreamReader(device);
964
965 XmlStreamReaderPointer pointer(reader);
966
967 // store the shared pointer in the script object that we are constructing
968 return engine->newVariant(context->thisObject(), qVariantFromValue(pointer));
969}
970//! [93]
971
972//! [94]
973QScriptValue xmlStreamReader_atEnd(QScriptContext *context, QScriptEngine *)
974{
975 XmlStreamReaderPointer reader = qscriptvalue_cast<XmlStreamReaderPointer>(context->thisObject());
976 if (!reader)
977 return context->throwError(QScriptContext::TypeError, "this object is not an XmlStreamReader");
978 return reader->atEnd();
979}
980//! [94]
981
982//! [95]
983 QScriptEngine engine;
984 QScriptValue xmlStreamReaderProto = engine.newObject();
985 xmlStreamReaderProto.setProperty("atEnd", engine.newFunction(xmlStreamReader_atEnd));
986
987 QScriptValue xmlStreamReaderCtor = engine.newFunction(constructXmlStreamReader, xmlStreamReaderProto);
988 engine.globalObject().setProperty("XmlStreamReader", xmlStreamReaderCtor);
989//! [95]
Note: See TracBrowser for help on using the repository browser.