summaryrefslogtreecommitdiff
path: root/src/controls/doc/src/qtquickcontrols-examples.qdoc
blob: b6f2dca09100192b29739941a785bc1f8045f2cc (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \group qtquickcontrols_examples
    \ingroup all-examples
    \title Qt Quick Controls 1 Examples
    \brief A Collection of examples for \l{Qt Quick Controls 1}, written in QML.

    These examples show how to create user interfaces using
    \l{Qt Quick Controls 1}.
*/

/*!
    \example gallery
    \title Qt Quick Controls 1 - Gallery
    \ingroup qtquickcontrols_examples
    \brief A collection of components for a classic desktop-style UI.

    \raw HTML
    <div class="table"><table style="background:transparent; border:0px">
    <tr><td style="border:0px">
    \endraw
    \image qtquickcontrols-example-gallery-osx.png
    \caption \macos
    \raw HTML
    </td><td style="border:0px">
    \endraw
    \image qtquickcontrols-example-gallery-android.png
    \caption Android - Nexus 5
    \raw HTML
    </td></tr>
    </table></div>
    \endraw

    This example project demonstrates the various UI components provided by
    \l{Qt Quick Controls 1}.

    \include examples-run.qdocinc
*/

/*!
    \example uiforms
    \title Qt Quick Controls 1 - UI Forms
    \ingroup qtquickcontrols_examples
    \brief Demonstrates how to separate the application logic from the UI.

    \image qtquickcontrols-example-uiforms.png

    \e{UI Forms} demonstrates how to separate the application logic
    from the UI using \e ui.qml files. The example is a simple interface to a customer
    database, purely written in QML and JavaScript.

    UI Forms are rigorously split into \e .qml and \e .js files that contain the business logic, and \e .ui.qml
    files that only contain the purely declarative description of the UI.
    The \e .ui.qml files act as forms and they should be only edited in the Design mode of Qt Creator.

    \section1 Exporting Items from Forms

    In all forms, items that are supposed to interact with the application logic are exported:

    \code
    property alias cancel: cancel
    property alias save: save
    property alias textArea: textArea
    \endcode

    This is the way the items are exported in \e NotesForm.ui.qml, so they can be used in
    \e Notes.ui.qml to implement the logic as follows:

    \code
    function readData() {
        CustomerModel.selection.forEach(function (rowIndex) {
            form.textArea.text = CustomerModel.model.get(rowIndex).notes
        })

        save.enabled = true
        cancel.enabled = true
        form.textArea.enabled = true
    }

    function writeData() {
        CustomerModel.selection.forEach(function (rowIndex) {
            var data = CustomerModel.model.get(rowIndex)
            data.notes = form.textArea.text
            CustomerModel.model.set(rowIndex, data)
        })
    }

    cancel.onClicked: readData()
    save.onClicked: writeData()
    \endcode

    \section1 Implementing the Backend in a Singleton

    Because the ListModel is accessed from several different \e .qml files, we access the
    ListModel through a singleton defined in \e CustomerModel.qml and registered in \e main.ccp.
    This way we do not have to rely on the QML context scoping rules to access the ListModel.

    \include examples-run.qdocinc
*/

/*!
    \example tableview
    \title Qt Quick Controls 1 - Table View Example
    \ingroup qtquickcontrols_examples
    \brief An example for the TableView control.
    \image qtquickcontrols-example-tableview.png

    This example project demonstrates the usage of \l {TableView} from
    \l{Qt Quick Controls 1} - a control to display one or more columns of
    information from a data list model. The example includes a model
    that supports sorting and filtering.

    The C++ class, SortFilterProxyModel, is registered as a QML type
    under the namespace, "\c{org.qtproject.example 1.0}".

    The following snippets show how the type is registered under
    a namespace and later imported by \e main.qml.

    QML type registration:

    \code
    #include <QtQml/qqml.h>
    ...
    qmlRegisterType<SortFilterProxyModel>("org.qtproject.example", 1, 0, "SortFilterProxyModel");
    ...
    \endcode

    QML namespace import:

    \code
    import org.qtproject.example 1.0
    \endcode

    For more information about registering C++ classes as QML types, see
    \l {Defining QML Types from C++}.

    \include examples-run.qdocinc
*/

/*!
    \example texteditor
    \title Qt Quick Controls 1 - Text Editor Example
    \ingroup qtquickcontrols_examples
    \brief A QML app using Qt Quick Controls and a C++ class to
    provide a fully-functional rich-text editor application.

    \image qtquickcontrols-example-text.png

    The \e{Text Editor Example} presents a sample HTML file using the TextArea
    control, preserving the HTML formatting. It uses a C++ class to handle the
    document by providing options to open, format, and edit. The app also lets
    you open and edit a plain text files.

    The C++ class, DocumentHandler, extends QObject and is registered
    as a QML type under the namespace, "\c{org.qtproject.example 1.0}".

    The following snippets show how the type is registered under
    a namespace and later imported by \e main.qml.

    QML type registration:

    \code
    #include <QtQml/qqml.h>
    ...
    qmlRegisterType<DocumentHandler>("org.qtproject.example", 1, 0, "DocumentHandler");
    ...
    \endcode

    QML namespace import:

    \code
    import org.qtproject.example 1.0
    \endcode

    For more information about registering C++ classes as QML types, see
    \l {Defining QML Types from C++}.

    \include examples-run.qdocinc
*/

/*!
    \example touch
    \title Qt Quick Controls 1 - Touch Gallery
    \ingroup qtquickcontrols_examples
    \brief Demonstrates UI controls for a touch interface.
    \image qtquickcontrols-example-touch.png

    \e {Touch Gallery} demonstrates how to implement a UI suitable for touch
    input using the following \l{Qt Quick Controls 1}:

    \list
        \li \l ApplicationWindow
        \li \l Button
        \li \l ProgressBar
        \li \l ScrollView
        \li \l Slider
        \li \l StackView
        \li \l Switch
        \li \l Tab
        \li \l TabView
        \li \l TextField
    \endlist

    The appearance of the controls is customized by using
    \l {Qt Quick Controls 1 Styles}.

    \include examples-run.qdocinc

    \section1 Creating the Main Page

    In the main.qml file, we use a \l Rectangle type within the
    \l ApplicationWindow type to create the main page of the application:

    \quotefromfile touch/main.qml
    \skipto ApplicationWindow
    \printuntil }

    To use the Qt Quick Controls, we must import them:

    \code
    import QtQuick.Controls 1.2
    \endcode

    The \c toolBar property of the application window holds a BorderImage type
    that we use to create a separator between the application name and a list of
    additional pages:

    \quotefromfile touch/main.qml
    \skipto toolBar
    \printuntil height

    We use an \l Image type in a \l Rectangle type to create a back button. We
    use the \c onClicked signal handler to call the StackView \c pop() function
    that pops off the page when users tap the button:

    \printuntil pop()
    \printuntil }
    \printuntil }

    We use the \c opacity property to hide the back button on the main page.

    We use a \l Text type to display the application name:

    \printuntil text:
    \printuntil }

    The \c x position of the \l Text type is bound to the position and width of
    the back button, and animated using a \l Behavior.

    We use a ListModel type that contains ListElement definitions to define
    titles and source files for the other pages in the application:

    \skipto ListModel
    \printuntil ListPage
    \printuntil }
    \printuntil }

    \section1 Navigating in the Application

    We implement a stack-based navigation model to link the application pages
    together. Items are pushed onto the stack as users navigate deeper into the
    application, and popped off again when they return to the main page.

    In main.qml, we add a \l StackView type as a child of the application
    window:

    \skipto StackView
    \printuntil }

    The stack is used by invoking its navigation methods. To load the first item
    in the stack view, we assign it to \c initialItem:

    \printuntil /^\}/

    We use a ListView type to display a list of the items provided by
    \c pageModel. The \c AndroidDelegate custom type defines each item
    instantiated by the view.

    \section1 Creating Push Buttons and Switches

    In ButtonPage.qml we use the \l Button type to create two buttons that
    change color when users tap them and one that pops off the page and returns
    the user to the main page:

    \quotefromfile touch/content/ButtonPage.qml
    \skipto Button
    \printuntil stackView
    \printuntil }

    We use a \l Switch type to create two switches that users can turn on and
    off. They are placed within a \l Row type to lay them out horizontally:

    \printuntil switchStyle
    \printuntil switchStyle
    \printuntil }
    \printuntil }

    A \l ButtonStyle type creates a custom appearance for the buttons:

    \skipto Component
    \printuntil }

    To use Qt Quick Controls Styles, we must import them:

    \code
    import QtQuick.Controls.Styles 1.1
    \endcode

    A \l SwitchStyle type creates a custom appearance for the switches:

    \quotefromfile touch/content/ButtonPage.qml
    \skipuntil Component
    \skipto Component
    \printuntil /^\}/

    The \c groove property holds the background groove of the switch and the
    \c handle property defines the switch handle.

    \section1 Creating Sliders

    In SliderPage.qml, we use a \l Slider type to create three horizontal
    sliders that are placed within a \l Column type to lay them out in a column:

    \quotefromfile touch/content/SliderPage.qml
    \skipto Column
    \printuntil 1.0
    \printuntil }
    \printuntil }

    The \c value property holds the initial handle position on the slider.

    A SliderStyle type creates a custom appearance for the sliders:

    \printuntil /^\}/

    The \c handle property defines the slider handle and the \c groove property
    holds the background groove of the slider.

    \section1 Indicating Progress

    In ProgressBar.qml, we use a ProgressBar type to create three progress bars:

    \quotefromfile touch/content/ProgressBarPage.qml
    \skipto Column
    \printuntil 1
    \printuntil 400
    \printuntil }

    We use a NumberAnimation type with a SequentialAnimation type to run two
    number animations in a sequence. We apply the animations on the \c progress
    custom property to animate the current value on the progress bars:

    \quotefromfile touch/content/ProgressBarPage.qml
    \skipto progress
    \printuntil }
    \printuntil }

    A ProgressBarStyle type creates a custom appearance for the progress bars:

    \quotefromfile touch/content/ProgressBarPage.qml
    \skipto Component
    \printuntil /^\}/

    \section1 Creating Tabs

    In TabBarPage.qml, we use a TabView type with a \l Tab type to provide a
    tab-based navigation model for our application. We use tabs to display the
    ButtonPage, SliderPage, and ProgressBarPage on separate tab pages:

    \quotefromfile touch/content/TabBarPage.qml
    \skipto TabView
    \printuntil ProgressBarPage
    \printuntil }

    A TabViewStyle type creates a custom appearance for the tabs:

    \skipto Component
    \printuntil /^\}/

    \section1 Creating Text Input Fields

    In the TextInputPage.qml, we use a TextField type to create an input field
    and a read-only text field:

    \quotefromfile touch/content/TextInputPage.qml
    \skipto Column
    \printuntil true
    \printuntil }
    \printuntil }

    A TextFieldStyle creates a custom appearance for the text fields:

    \printuntil /^\}/

    We use a BorderImage type with an image to create borders for the fields.

    \section1 Creating Scrolling Lists

    In ListPage.qml, we use a ScrollView type to provide a scrolling page with a
    vertical scoll bar:

    \quotefromfile touch/content/ListPage.qml
    \skipto ScrollView
    \printuntil true

    We use a ListView type to display a list of 100 items by specifying an
    \l{Models and Views in Qt Quick#Integers as Models}{integer} as the value of
    the \c model property. We reuse the \c AndroidDelegate custom type here to
    define each item instantiated by the view. The \c text property adds the
    string \c {Item #} to each list item:

    \printuntil }
    \printuntil }

    A ScrollViewStyle type creates a custom appearance for the scroll view:

    \printuntil /^\}/

    The \c transientScrollBars property is set to \c true to make the scroll
    bars appear when the content is scrolled and disappear when they are no
    longer needed.

    The \c handle property controls the appearance of the scroll bar handle and
    the \c scrollBarBackground property that of the background.
*/

/*!
    \example calendar
    \title Qt Quick Controls 1 - Calendar Example
    \ingroup qtquickcontrols_examples
    \brief Demonstrates the use of Calendar control.
    \image qtquickcontrols-example-calendar.png

    The Calendar example displays a Calendar control and an events list for
    the selected date. It uses a C++ class to fetch the event details from an
    SQLite database. The example app uses a custom CalendarStyle to highlight
    the selected date and mark the dates that have events.

    The following snippet from \e main.qml shows how the Calendar control is
    used in the app:

    \code
    Calendar {
        id: calendar
        width: (parent.width > parent.height ? parent.width * 0.6 - parent.spacing : parent.width)
        height: (parent.height > parent.width ? parent.height * 0.6 - parent.spacing : parent.height)
        frameVisible: true
        weekNumbersVisible: true
        selectedDate: new Date(2014, 0, 1)
        focus: true

        style: CalendarStyle {
            dayDelegate: Item {
                     ...
                }
           }
    }
    \endcode

    The C++ class, SqlEventModel, inherits SqlQueryModel to create a database
    with dummy events for certain dates.

    \quotefromfile calendar/src/sqleventmodel.cpp
    \skipto SqlEventModel::SqlEventModel()
    \printto QList
    \skipto void SqlEventModel
    \printuntil /^\}/

    In \e main.qml, the SqlEventModel custom type is used to get the list of
    events to mark the dates on the calendar.

    \code
    SqlEventModel {
        id: eventModel
    }

    Calendar {
        ...
        style: CalendarStyle {
            dayDelegate: Item {
                ...
                Image {
                    visible: eventModel.eventsForDate(styleData.date).length > 0
                    ...
                    source: "qrc:/images/eventindicator.png"
                }
            }
        }
    }
    \endcode

    The app uses a Flow type to position the items, and manipulates the
    items' width and height based on the orientation change on mobile devices.

    \code
    Calendar {
        id: calendar
        width: (parent.width > parent.height ? parent.width * 0.6 - parent.spacing : parent.width)
        height: (parent.height > parent.width ? parent.height * 0.6 - parent.spacing : parent.height)
    }

    Rectangle {
        width: (parent.width > parent.height ? parent.width * 0.4 - parent.spacing : parent.width)
        height: (parent.height > parent.width ? parent.height * 0.4 - parent.spacing : parent.height)
        border.color: Qt.darker(color, 1.2)

        ListView {
            ...
        }
    }
    \endcode
    \include examples-run.qdocinc
*/

/*!
    \example styles
    \title Qt Quick Controls 1 - Styles Example
    \ingroup qtquickcontrols_examples
    \brief Demonstrates custom styles.
    \image qtquickcontrols-example-styles.png

    This example shows how to create custom styles for \l{Qt Quick Controls 1}.

    \include examples-run.qdocinc
*/

/*!
    \example filesystembrowser
    \title Qt Quick Controls 1 - File System Browser Example
    \ingroup qtquickcontrols_examples
    \brief An example for the TreeView control.
    \image qtquickcontrols-example-filesystembrowser.png

    This example project demonstrates the usage of \l {TreeView} from
    \l{Qt Quick Controls 1} - a control to display a tree representation of items
    from a model derived from the QAbstractItemModel class.

    The example displays the home path data given by the QFileSystemModel model.
*/