source: trunk/doc/src/examples/qtscriptcustomclass.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 9.9 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the documentation of the Qt Toolkit.
8**
[846]9** $QT_BEGIN_LICENSE:FDL$
[2]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
[846]13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
[2]15**
[846]16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
[2]21**
[561]22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
[2]24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example script/customclass
30 \title Custom Script Class Example
31
32 The Custom Script Class example shows how to use QScriptClass and QScriptClassPropertyIterator
33 to implement a custom script class.
34
35 The script class we are going to implement is called \c{ByteArray}. It provides a wrapper around
36 the QByteArray class in Qt, with a simplified API. Why do we need such a class? Well, neither the
37 ECMAScript \c{Array} class or \c{String} class is appropriate to use when working with arrays of
38 bytes. Our \c{ByteArray} class will have the right semantics; objects will use only the amount of
39 memory that is really needed (a byte is stored as a byte, not as a floating-point number or a
40 Unicode character) and can be passed directly to C++ slots taking QByteArray arguments (no costly
41 conversion necessary).
42
43 \section1 ByteArray Class In Use
44
45 When the \c{ByteArray} class has been made available to the
46 scripting environment, \c{ByteArray} objects can be constructed like
47 so:
48
49 \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 0
50
51 \c{ByteArray} objects behave similar to normal \c{Array} objects. Every \c{ByteArray} object has
52 a \c{length} property, that holds the length of the array. If a new value is assigned to the \c{length}
53 property, the array is resized. If the array is enlarged, the new bytes are initialized to 0.
54 (This is a difference from normal \c{Array} objects; \c{ByteArray} objects are always dense arrays.)
55 Use normal array operations to read or write bytes in the array. The following code sets all the
56 bytes of an array to a certain value:
57
58 \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 1
59
60 When assigning a value to an array element, the value is truncated to eight bits:
61
62 \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 2
63
64 Like normal \c{Array} objects, if the array index is greater than the current length