1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:FDL$
|
---|
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 a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
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.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example script/qstetrix
|
---|
30 | \title Qt Script Tetrix Example
|
---|
31 |
|
---|
32 | The QSTetrix example is a Qt Script version of the classic Tetrix game.
|
---|
33 |
|
---|
34 | \image tetrix-example.png
|
---|
35 |
|
---|
36 | \section1 Overview
|
---|
37 |
|
---|
38 | The program logic in this example is a fairly straight port of the
|
---|
39 | logic in the C++ \l{Tetrix Example}. You may find it useful to compare
|
---|
40 | the implementations of the \c TetrixBoard, \c TetrixPiece and
|
---|
41 | \c TetrixWindow classes to see how Qt Script is used to implement
|
---|
42 | methods, call Qt functions, and emit signals.
|
---|
43 |
|
---|
44 | \section1 Setting up the GUI
|
---|
45 |
|
---|
46 | The graphical user interface is defined in a UI file, created
|
---|
47 | using Qt Designer, and is set up in the example's C++ \c{main.cpp} file.
|
---|
48 |
|
---|
49 | \snippet examples/script/qstetrix/main.cpp 0
|
---|
50 |
|
---|
51 | We define a custom UI loader that handles our \c TetrixBoard widget; this
|
---|
52 | is the main component of the UI (where the pieces are drawn).
|
---|
53 |
|
---|
54 | \snippet examples/script/qstetrix/main.cpp 1
|
---|
55 |
|
---|
56 | We initialize the script engine to have the Qt namespace, so that
|
---|
57 | e.g., \l{Qt::Key_Left}{Qt.Key_Left} will be available to script code.
|
---|
58 | We also make the application object available (for the
|
---|
59 | \l{QApplication::}{quit()} slot).
|
---|
60 |
|
---|
61 | \snippet examples/script/qstetrix/main.cpp 2
|
---|
62 |
|
---|
63 | Several scripts are evaluated as part of the engine setup process.
|
---|
64 | The \c{tetrixpiece.js} file contains the definition of the \c TetrixPiece
|
---|
65 | class, which is used to populate the play field. The \c{tetrixboard.js}
|
---|
66 | file contains the definition of the \c TetrixBoard class, which contains
|
---|
67 | the main game logic. Finally, \c{tetrixwindow.js} contains the definition
|
---|
68 | of the \c TetrixWindow class, which wires up the top-level widget.
|
---|
69 |
|
---|
70 | \snippet examples/script/qstetrix/main.cpp 3
|
---|
71 |
|
---|
72 | A form is created from the UI file. A new \c TetrixWindow script object
|
---|
73 | is then constructed, passing the form as its argument.
|
---|
74 |
|
---|
75 | \snippet examples/script/qstetrix/main.cpp 4
|
---|
76 |
|
---|
77 | The form is shown, and the event loop is entered.
|
---|
78 | */
|
---|