1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example richtext/orderform
30 \title Order Form Example
32 \brief The Order Form example shows how to generate rich text documents by
33 combining a simple template with data input by the user in a dialog.
35 Data is extracted from a \c DetailsDialog object and displayed on a QTextEdit
36 with a QTextCursor, using various formats. Each form generated is added
37 to a QTabWidget for easy access.
39 \image orderform-example.png
41 \section1 DetailsDialog Definition
43 The \c DetailsDialog class is a subclass of QDialog, implementing a slot
44 \c verify() to allow contents of the \c DetailsDialog to be verified later.
45 This is further explained in \c DetailsDialog Implementation.
47 \snippet examples/richtext/orderform/detailsdialog.h 0
49 The constructor of \c DetailsDialog accepts parameters \a title and
50 \a parent. The class defines four \e{getter} functions: \c orderItems(),
51 \c senderName(), \c senderAddress(), and \c sendOffers() to allow data
52 to be accessed externally.
54 The class definition includes input widgets for the required
55 fields, \c nameEdit and \c addressEdit. Also, a QCheckBox and a
56 QDialogButtonBox are defined; the former to provide the user with the
57 option to receive information on products and offers, and the latter
58 to ensure that buttons used are arranged according to the user's native
59 platform. In addition, a QTableWidget, \c itemsTable, is used to hold
62 The screenshot below shows the \c DetailsDialog we intend to create.
64 \image orderform-example-detailsdialog.png
66 \section1 DetailsDialog Implementation
68 The constructor of \c DetailsDialog instantiates the earlier defined fields
69 and their respective labels. The label for \c offersCheckBox is set and the
70 \c setupItemsTable() function is invoked to setup and populate
71 \c itemsTable. The QDialogButtonBox object, \c buttonBox, is instantiated
72 with \gui OK and \gui Cancel buttons. This \c buttonBox's \c accepted() and
73 \c rejected() signals are connected to the \c verify() and \c reject()
74 slots in \c DetailsDialog.
76 \snippet examples/richtext/orderform/detailsdialog.cpp 0
78 A QGridLayout is used to place all the objects on the \c DetailsDialog.
80 \snippet examples/richtext/orderform/detailsdialog.cpp 1
82 The \c setupItemsTable() function instantiates the QTableWidget object,
83 \c itemsTable, and sets the number of rows based on the QStringList
84 object, \c items, which holds the type of items ordered. The number of
85 columns is set to 2, providing a "name" and "quantity" layout. A \c for
86 loop is used to populate the \c itemsTable and the \c name item's flag
87 is set to Qt::ItemIsEnabled or Qt::ItemIsSelectable. For demonstration
88 purposes, the \c quantity item is set to a 1 and all items in the
89 \c itemsTable have this value for quantity; but this can be modified by
90 editing the contents of the cells at run time.
92 \snippet examples/richtext/orderform/detailsdialog.cpp 2
94 The \c orderItems() function extracts data from the \c itemsTable and
95 returns it in the form of a QList<QPair<QString,int>> where each QPair
96 corresponds to an item and the quantity ordered.
98 \snippet examples/richtext/orderform/detailsdialog.cpp 3
100 The \c senderName() function is used to return the value of the QLineEdit
101 used to store the name field for the order form.
103 \snippet examples/richtext/orderform/detailsdialog.cpp 4
105 The \c senderAddress() function is used to return the value of the
106 QTextEdit containing the address for the order form.
108 \snippet examples/richtext/orderform/detailsdialog.cpp 5
110 The \c sendOffers() function is used to return a \c true or \c false
111 value that is used to determine if the customer in the order form
112 wishes to receive more information on the company's offers and promotions.
114 \snippet examples/richtext/orderform/detailsdialog.cpp 6
116 The \c verify() function is an additionally implemented slot used to
117 verify the details entered by the user into the \c DetailsDialog. If
118 the details entered are incomplete, a QMessageBox is displayed
119 providing the user the option to discard the \c DetailsDialog. Otherwise,
120 the details are accepted and the \c accept() function is invoked.
122 \snippet examples/richtext/orderform/detailsdialog.cpp 7
124 \section1 MainWindow Definition
126 The \c MainWindow class is a subclass of QMainWindow, implementing two
127 slots - \c openDialog() and \c printFile(). It also contains a private
128 instance of QTabWidget, \c letters.
130 \snippet examples/richtext/orderform/mainwindow.h 0
132 \section1 MainWindow Implementation
134 The \c MainWindow constructor sets up the \c fileMenu and the required
135 actions, \c newAction and \c printAction. These actions' \c triggered()
136 signals are connected to the additionally implemented openDialog() slot
137 and the default close() slot. The QTabWidget, \c letters, is
138 instantiated and set as the window's central widget.
140 \snippet examples/richtext/orderform/mainwindow.cpp 0
142 The \c createLetter() function creates a new QTabWidget with a QTextEdit,
143 \c editor, as the parent. This function accepts four parameters that
144 correspond to we obtained through \c DetailsDialog, in order to "fill"
147 \snippet examples/richtext/orderform/mainwindow.cpp 1
149 We then obtain the cursor for the \c editor using QTextEdit::textCursor().
150 The \c cursor is then moved to the start of the document using
153 \snippet examples/richtext/orderform/mainwindow.cpp 2
155 Recall the structure of a \l{Rich Text Document Structure}
156 {Rich Text Document}, where sequences of frames and
157 tables are always separated by text blocks, some of which may contain no
160 In the case of the Order Form Example, the document structure for this portion
161 is described by the table below:
165 \o {1, 8} frame with \e{referenceFrameFormat}
167 \o block \o \c{A company}
171 \o block \o \c{321 City Street}
175 \o block \o \c{Industry Park}
179 \o block \o \c{Another country}
182 This is accomplished with the following code:
184 \snippet examples/richtext/orderform/mainwindow.cpp 3
186 Note that \c topFrame is the \c {editor}'s top-level frame and is not shown
187 in the document structure.
189 We then set the \c{cursor}'s position back to its last position in
190 \c topFrame and fill in the customer's name (provided by the constructor)
191 and address - using a \c foreach loop to traverse the QString, \c address.
193 \snippet examples/richtext/orderform/mainwindow.cpp 4
195 The \c cursor is now back in \c topFrame and the document structure for
196 the above portion of code is:
200 \o block \o \c{Donald}
202 \o block \o \c{47338 Park Avenue}
204 \o block \o \c{Big City}
207 For spacing purposes, we invoke \l{QTextCursor::insertBlock()}
208 {insertBlock()} twice. The \l{QDate::currentDate()}{currentDate()} is
209 obtained and displayed. We use \l{QTextFrameFormat::setWidth()}
210 {setWidth()} to increase the width of \c bodyFrameFormat and we insert
211 a new frame with that width.
213 \snippet examples/richtext/orderform/mainwindow.cpp 5
215 The following code inserts standard text into the order form.
217 \snippet examples/richtext/orderform/mainwindow.cpp 6
218 \snippet examples/richtext/orderform/mainwindow.cpp 7
220 This part of the document structure now contains the date, a frame with
221 \c bodyFrameFormat, as well as the standard text.
229 \o block \o \c{Date: 25 May 2007}
233 \o {1, 4} frame with \e{bodyFrameFormat}
235 \o block \o \c{I would like to place an order for the following items:}
242 A QTextTableFormat object, \c orderTableFormat, is used to hold the type
243 of item and the quantity ordered.
245 \snippet examples/richtext/orderform/mainwindow.cpp 8
247 We use \l{QTextTable::cellAt()}{cellAt()} to set the headers for the
250 \snippet examples/richtext/orderform/mainwindow.cpp 9
252 Then, we iterate through the QList of QPair objects to populate
255 \snippet examples/richtext/orderform/mainwindow.cpp 10
257 The resulting document structure for this section is:
261 \o {1, 11} \c{orderTable} with \e{orderTableFormat}
263 \o block \o \c{Product}
265 \o block \o \c{Quantity}
267 \o block \o \c{T-shirt}
271 \o block \o \c{Badge}
275 \o block \o \c{Reference book}
279 \o block \o \c{Coffee cup}
284 The \c cursor is then moved back to \c{topFrame}'s
285 \l{QTextFrame::lastPosition()}{lastPosition()} and more standard text
288 \snippet examples/richtext/orderform/mainwindow.cpp 11
289 \snippet examples/richtext/orderform/mainwindow.cpp 12
291 Another QTextTable is inserted, to display the customer's
292 preference regarding offers.
294 \snippet examples/richtext/orderform/mainwindow.cpp 13
296 The document structure for this portion is:
302 \o block\o \c{Please update my...}
306 \o {1, 4} \c{offersTable}
308 \o block \o \c{I want to receive...}
310 \o block \o \c{I do not want to recieve...}
315 The \c cursor is moved to insert "Sincerely" along with the customer's
316 name. More blocks are inserted for spacing purposes. The \c printAction
317 is enabled to indicate that an order form can now be printed.
319 \snippet examples/richtext/orderform/mainwindow.cpp 14
321 The bottom portion of the document structure is:
327 \o {1, 5} block\o \c{Sincerely,}
335 \o block \o \c{Donald}
338 The \c createSample() function is used for illustration purposes, to create
341 \snippet examples/richtext/orderform/mainwindow.cpp 15
343 The \c openDialog() function opens a \c DetailsDialog object. If the
344 details in \c dialog are accepted, the \c createLetter() function is
345 invoked using the parameters extracted from \c dialog.
347 \snippet examples/richtext/orderform/mainwindow.cpp 16
349 In order to print out the order form, a \c printFile() function is
350 included, as shown below:
352 \snippet examples/richtext/orderform/mainwindow.cpp 17
354 This function also allows the user to print a selected area with
355 QTextCursor::hasSelection(), instead of printing the entire document.
357 \section1 \c main() Function
359 The \c main() function instantiates \c MainWindow and sets its size to
360 640x480 pixels before invoking the \c show() function and
361 \c createSample() function.
363 \snippet examples/richtext/orderform/main.cpp 0