Debian Codegraph @

src/script/doc/snippets/code/doc_src_qtscript.js

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 //! [2]
52 function myInterestingScriptFunction() {
53 // ...
54 }
55 // ...
56 myQObject.somethingChanged.connect(myInterestingScriptFunction);
57 //! [2]
58
59
60 //! [3]
61 myQObject.somethingChanged.connect(myOtherQObject.doSomething);
62 //! [3]
63
64
65 //! [4]
66 myQObject.somethingChanged.disconnect(myInterestingFunction);
67 myQObject.somethingChanged.disconnect(myOtherQObject.doSomething);
68 //! [4]
69
70
71 //! [5]
72 var obj = { x: 123 };
73 var fun = function() { print(this.x); };
74 myQObject.somethingChanged.connect(obj, fun);
75 //! [5]
76
77
78 //! [6]
79 myQObject.somethingChanged.disconnect(obj, fun);
80 //! [6]
81
82
83 //! [7]
84 var obj = { x: 123, fun: function() { print(this.x); } };
85 myQObject.somethingChanged.connect(obj, "fun");
86 //! [7]
87
88
89 //! [8]
90 myQObject.somethingChanged.disconnect(obj, "fun");
91 //! [8]
92
93
94 //! [9]
95 try {
96 myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist");
97 } catch (e) {
98 print(e);
99 }
100 //! [9]
101
102
103 //! [10]
104 myQObject.somethingChanged("hello");
105 //! [10]
106
107
108 //! [11]
109 myQObject.myOverloadedSlot(10); // will call the int overload
110 myQObject.myOverloadedSlot("10"); // will call the QString overload
111 //! [11]
112
113
114 //! [12]
115 myQObject['myOverloadedSlot(int)']("10"); // call int overload; the argument is converted to an int
116 myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string
117 //! [12]
118
119
120 //! [14]
121 myQObject.enabled = true;
122
123 // ...
124
125 myQObject.enabled = !myQObject.enabled;
126 //! [14]
127
128
129 //! [15]
130 myDialog.okButton
131 //! [15]
132
133
134 //! [16]
135 myDialog.okButton.objectName = "cancelButton";
136 // from now on, myDialog.cancelButton references the button
137 //! [16]
138
139
140 //! [17]
141 var okButton = myDialog.findChild("okButton");
142 if (okButton != null) {
143 // do something with the OK button
144 }
145
146 var buttons = myDialog.findChildren(RegExp("button[0-9]+"));
147 for (var i = 0; i < buttons.length; ++i) {
148 // do something with buttons[i]
149 }
150 //! [17]
151
152
153 //! [21]
154 var obj = new MyObject;
155 obj.setEnabled( true );
156 print( "obj is enabled: " + obj.isEnabled() );
157 //! [21]
158
159
160 //! [22]
161 var obj = new MyObject;
162 obj.enabled = true;
163 print( "obj is enabled: " + obj.enabled );
164 //! [22]
165
166
167 //! [26]
168 function enabledChangedHandler( b )
169 {
170 print( "state changed to: " + b );
171 }
172
173 function init()
174 {
175 var obj = new MyObject();
176 // connect a script function to the signal
177 obj["enabledChanged(bool)"].connect(enabledChangedHandler);
178 obj.enabled = true;
179 print( "obj is enabled: " + obj.enabled );
180 }
181 //! [26]
182
183
184 //! [27]
185 var o = new Object();
186 o.foo = 123;
187 print(o.hasOwnProperty('foo')); // true
188 print(o.hasOwnProperty('bar')); // false
189 print(o); // calls o.toString(), which returns "[object Object]"
190 //! [27]
191
192
193 //! [28]
194 function Person(name)
195 {
196 this.name = name;
197 }
198 //! [28]
199
200
201 //! [29]
202 Person.prototype.toString = function() { return "Person(name: " + this.name + ")"; }
203 //! [29]
204
205
206 //! [30]
207 var p1 = new Person("John Doe");
208 var p2 = new Person("G.I. Jane");
209 print(p1); // "Person(name: John Doe)"
210 print(p2); // "Person(name: G.I. Jane)"
211 //! [30]
212
213
214 //! [31]
215 print(p1.hasOwnProperty('name')); // 'name' is an instance variable, so this returns true
216 print(p1.hasOwnProperty('toString')); // returns false; inherited from prototype
217 print(p1 instanceof Person); // true
218 print(p1 instanceof Object); // true
219 //! [31]
220
221
222 //! [32]
223 function Employee(name, salary)
224 {
225 Person.call(this, name); // call base constructor
226
227 this.salary = salary;
228 }
229
230 // set the prototype to be an instance of the base class
231 Employee.prototype = new Person();
232
233 // initialize prototype
234 Employee.prototype.toString = function() {
235 // ...
236 }
237 //! [32]
238
239
240 //! [33]
241 var e = new Employee("Johnny Bravo", 5000000);
242 print(e instanceof Employee); // true
243 print(e instanceof Person); // true
244 print(e instanceof Object); // true
245 print(e instanceof Array); // false
246 //! [33]
247
248
249 //! [40]
250 var o = new Object();
251 (o.__proto__ === Object.prototype); // this evaluates to true
252 //! [40]
253
254
255 //! [41]
256 var o = new Object();
257 o.__defineGetter__("x", function() { return 123; });
258 var y = o.x; // 123
259 //! [41]
260
261
262 //! [42]
263 var o = new Object();
264 o.__defineSetter__("x", function(v) { print("and the value is:", v); });
265 o.x = 123; // will print "and the value is: 123"
266 //! [42]
267
268
269 //! [49]
270 var getProperty = function(name) { return this[name]; };
271
272 name = "Global Object"; // creates a global variable
273 print(getProperty("name")); // "Global Object"
274
275 var myObject = { name: 'My Object' };
276 print(getProperty.call(myObject, "name")); // "My Object"
277
278 myObject.getProperty = getProperty;
279 print(myObject.getProperty("name")); // "My Object"
280
281 getProperty.name = "The getProperty() function";
282 getProperty.getProperty = getProperty;
283 getProperty.getProperty("name"); // "The getProperty() function"
284 //! [49]
285
286 //! [50]
287 var o = { a: 1, b: 2, sum: function() { return a + b; } };
288 print(o.sum()); // reference error, or sum of global variables a and b!!
289 //! [50]
290
291 //! [51]
292 var o = { a: 1, b: 2, sum: function() { return this.a + this.b; } };
293 print(o.sum()); // 3
294 //! [51]
295
296 //! [56]
297 function add(a, b) {
298 return a + b;
299 }
300 //! [56]
301
302 //! [57]
303 function add() {
304 return arguments[0] + arguments[1];
305 }
306 //! [57]
307
308 //! [59]
309 function add() {
310 if (arguments.length != 2)
311 throw Error("add() takes exactly two arguments");
312 return arguments[0] + arguments[1];
313 }
314 //! [59]
315
316 //! [60]
317 function add() {
318 if (arguments.length != 2)
319 throw Error("add() takes exactly two arguments");
320 if (typeof arguments[0] != "number")
321 throw TypeError("add(): first argument is not a number");
322 if (typeof arguments[1] != "number")
323 throw TypeError("add(): second argument is not a number");
324 return arguments[0] + arguments[1];
325 }
326 //! [60]
327
328 //! [61]
329 function add() {
330 if (arguments.length != 2)
331 throw Error("add() takes exactly two arguments");
332 return Number(arguments[0]) + Number(arguments[1]);
333 }
334 //! [61]
335
336 //! [64]
337 function concat() {
338 var result = "";
339 for (var i = 0; i < arguments.length; ++i)
340 result += String(arguments[i]);
341 return result;
342 }
343 //! [64]
344
345 //! [66]
346 function sort(comparefn) {
347 if (comparefn == undefined)
348 comparefn = fn; /* replace fn with the built-in comparison function */
349 else if (typeof comparefn != "function")
350 throw TypeError("sort(): argument must be a function");
351 // ...
352 }
353 //! [66]
354
355 //! [68]
356 function foo() {
357 // Let bar() take care of this.
358 print("calling bar() with " + arguments.length + "arguments");
359 var result = bar.apply(this, arguments);
360 print("bar() returned" + result);
361 return result;
362 }
363 //! [68]
364
365 //! [70]
366 function counter() {
367 var count = 0;
368 return function() {
369 return count++;
370 }
371 }
372 //! [70]
373
374 //! [71]
375 var c1 = counter(); // create a new counter function
376 var c2 = counter(); // create a new counter function
377 print(c1()); // 0
378 print(c1()); // 1
379 print(c2()); // 0
380 print(c2()); // 1
381 //! [71]
382
383 //! [75]
384 function Book(isbn) {
385 this.isbn = isbn;
386 }
387
388 var coolBook1 = new Book("978-0131872493");
389 var coolBook2 = new Book("978-1593271473");
390 //! [75]
391
392 //! [80]
393 obj.x = "Roberta sent me";
394 print(obj.x); // "Ken sent me"
395 obj.x = "I sent the bill to Roberta";
396 print(obj.x); // "I sent the bill to Ken"
397 //! [80]
398
399 //! [81]
400 obj = {};
401 obj.__defineGetter__("x", function() { return this._x; });
402 obj.__defineSetter__("x", function(v) { print("setting x to", v); this._x = v; });
403 obj.x = 123;
404 //! [81]
405
406 //! [82]
407 myButton.text = qsTr("Hello world!");
408 //! [82]
409
410 //! [83]
411 myButton.text = qsTranslate("MyAwesomeScript", "Hello world!");
412 //! [83]
413
414 //! [84]
415 FriendlyConversation.prototype.greeting = function(type)
416 {
417 if (FriendlyConversation['greeting_strings'] == undefined) {
418 FriendlyConversation['greeting_strings'] = [
419 QT_TR_NOOP("Hello"),
420 QT_TR_NOOP("Goodbye")
421 ];
422 }
423 return qsTr(FriendlyConversation.greeting_strings[type]);
424 }
425 //! [84]
426
427 //! [85]
428 FriendlyConversation.prototype.greeting = function(type)
429 {
430 if (FriendlyConversation['greeting_strings'] == undefined) {
431 FriendlyConversation['greeting_strings'] = [
432 QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
433 QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
434 ];
435 }
436 return qsTranslate("FriendlyConversation", FriendlyConversation.greeting_strings[type]);
437 }
438 //! [85]
439
440 //! [86]
441 FileCopier.prototype.showProgress = function(done, total, currentFileName)
442 {
443 this.label.text = qsTr("%1 of %2 files copied.\nCopying: %3")
444 .arg(done)
445 .arg(total)
446 .arg(currentFileName);
447 }
448 //! [86]
449
450 //! [90]
451 ({ unitName: "Celsius",
452 toKelvin: function(x) { return x + 273; }
453 })
454 //! [90]