| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
function myInterestingScriptFunction() { |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
myQObject.somethingChanged.connect(myInterestingScriptFunction); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
myQObject.somethingChanged.connect(myOtherQObject.doSomething); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
myQObject.somethingChanged.disconnect(myInterestingFunction); |
| 67 |
myQObject.somethingChanged.disconnect(myOtherQObject.doSomething); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
var obj = { x: 123 }; |
| 73 |
var fun = function() { print(this.x); }; |
| 74 |
myQObject.somethingChanged.connect(obj, fun); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
myQObject.somethingChanged.disconnect(obj, fun); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
var obj = { x: 123, fun: function() { print(this.x); } }; |
| 85 |
myQObject.somethingChanged.connect(obj, "fun"); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
myQObject.somethingChanged.disconnect(obj, "fun"); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
try { |
| 96 |
myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist"); |
| 97 |
} catch (e) { |
| 98 |
print(e); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
myQObject.somethingChanged("hello"); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
myQObject.myOverloadedSlot(10); |
| 110 |
myQObject.myOverloadedSlot("10"); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
myQObject['myOverloadedSlot(int)']("10"); |
| 116 |
myQObject['myOverloadedSlot(QString)'](10); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
myQObject.enabled = true; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
myQObject.enabled = !myQObject.enabled; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
myDialog.okButton |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
myDialog.okButton.objectName = "cancelButton"; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
var okButton = myDialog.findChild("okButton"); |
| 142 |
if (okButton != null) { |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
var buttons = myDialog.findChildren(RegExp("button[0-9]+")); |
| 147 |
for (var i = 0; i < buttons.length; ++i) { |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
var obj = new MyObject; |
| 155 |
obj.setEnabled( true ); |
| 156 |
print( "obj is enabled: " + obj.isEnabled() ); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
var obj = new MyObject; |
| 162 |
obj.enabled = true; |
| 163 |
print( "obj is enabled: " + obj.enabled ); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
function enabledChangedHandler( b ) |
| 169 |
{ |
| 170 |
print( "state changed to: " + b ); |
| 171 |
} |
| 172 |
|
| 173 |
function init() |
| 174 |
{ |
| 175 |
var obj = new MyObject(); |
| 176 |
|
| 177 |
obj["enabledChanged(bool)"].connect(enabledChangedHandler); |
| 178 |
obj.enabled = true; |
| 179 |
print( "obj is enabled: " + obj.enabled ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
var o = new Object(); |
| 186 |
o.foo = 123; |
| 187 |
print(o.hasOwnProperty('foo')); |
| 188 |
print(o.hasOwnProperty('bar')); |
| 189 |
print(o); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
function Person(name) |
| 195 |
{ |
| 196 |
this.name = name; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
Person.prototype.toString = function() { return "Person(name: " + this.name + ")"; } |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
var p1 = new Person("John Doe"); |
| 208 |
var p2 = new Person("G.I. Jane"); |
| 209 |
print(p1); |
| 210 |
print(p2); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
print(p1.hasOwnProperty('name')); |
| 216 |
print(p1.hasOwnProperty('toString')); |
| 217 |
print(p1 instanceof Person); |
| 218 |
print(p1 instanceof Object); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
function Employee(name, salary) |
| 224 |
{ |
| 225 |
Person.call(this, name); |
| 226 |
|
| 227 |
this.salary = salary; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
Employee.prototype = new Person(); |
| 232 |
|
| 233 |
|
| 234 |
Employee.prototype.toString = function() { |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
var e = new Employee("Johnny Bravo", 5000000); |
| 242 |
print(e instanceof Employee); |
| 243 |
print(e instanceof Person); |
| 244 |
print(e instanceof Object); |
| 245 |
print(e instanceof Array); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
var o = new Object(); |
| 251 |
(o.__proto__ === Object.prototype); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
var o = new Object(); |
| 257 |
o.__defineGetter__("x", function() { return 123; }); |
| 258 |
var y = o.x; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
var o = new Object(); |
| 264 |
o.__defineSetter__("x", function(v) { print("and the value is:", v); }); |
| 265 |
o.x = 123; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
var getProperty = function(name) { return this[name]; }; |
| 271 |
|
| 272 |
name = "Global Object"; |
| 273 |
print(getProperty("name")); |
| 274 |
|
| 275 |
var myObject = { name: 'My Object' }; |
| 276 |
print(getProperty.call(myObject, "name")); |
| 277 |
|
| 278 |
myObject.getProperty = getProperty; |
| 279 |
print(myObject.getProperty("name")); |
| 280 |
|
| 281 |
getProperty.name = "The getProperty() function"; |
| 282 |
getProperty.getProperty = getProperty; |
| 283 |
getProperty.getProperty("name"); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
var o = { a: 1, b: 2, sum: function() { return a + b; } }; |
| 288 |
print(o.sum()); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
var o = { a: 1, b: 2, sum: function() { return this.a + this.b; } }; |
| 293 |
print(o.sum()); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
function add(a, b) { |
| 298 |
return a + b; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
function add() { |
| 304 |
return arguments[0] + arguments[1]; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
function add() { |
| 310 |
if (arguments.length != 2) |
| 311 |
throw Error("add() takes exactly two arguments"); |
| 312 |
return arguments[0] + arguments[1]; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 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 |
|
| 327 |
|
| 328 |
|
| 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 |
|
| 335 |
|
| 336 |
|
| 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 |
|
| 344 |
|
| 345 |
|
| 346 |
function sort(comparefn) { |
| 347 |
if (comparefn == undefined) |
| 348 |
comparefn = fn; |
| 349 |
else if (typeof comparefn != "function") |
| 350 |
throw TypeError("sort(): argument must be a function"); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
function foo() { |
| 357 |
|
| 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 |
|
| 364 |
|
| 365 |
|
| 366 |
function counter() { |
| 367 |
var count = 0; |
| 368 |
return function() { |
| 369 |
return count++; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
var c1 = counter(); |
| 376 |
var c2 = counter(); |
| 377 |
print(c1()); |
| 378 |
print(c1()); |
| 379 |
print(c2()); |
| 380 |
print(c2()); |
| 381 |
|
| 382 |
|
| 383 |
|
| 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 |
|
| 391 |
|
| 392 |
|
| 393 |
obj.x = "Roberta sent me"; |
| 394 |
print(obj.x); |
| 395 |
obj.x = "I sent the bill to Roberta"; |
| 396 |
print(obj.x); |
| 397 |
|
| 398 |
|
| 399 |
|
| 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 |
|
| 405 |
|
| 406 |
|
| 407 |
myButton.text = qsTr("Hello world!"); |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
myButton.text = qsTranslate("MyAwesomeScript", "Hello world!"); |
| 412 |
|
| 413 |
|
| 414 |
|
| 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 |
|
| 426 |
|
| 427 |
|
| 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 |
|
| 439 |
|
| 440 |
|
| 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 |
|
| 449 |
|
| 450 |
|
| 451 |
({ unitName: "Celsius", |
| 452 |
toKelvin: function(x) { return x + 273; } |
| 453 |
}) |
| 454 |
|