1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
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 qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example itemviews/simplewidgetmapper
|
---|
30 | \title Simple Widget Mapper Example
|
---|
31 |
|
---|
32 | The Simple Widget Mapper example shows how to use a widget mapper to display
|
---|
33 | data from a model in a collection of widgets.
|
---|
34 |
|
---|
35 | \image simplewidgetmapper-example.png
|
---|
36 |
|
---|
37 | The QDataWidgetMapper class allows information obtained from a
|
---|
38 | \l{Model Classes}{model} to be viewed and edited in a collection of
|
---|
39 | widgets instead of in an \l{View Classes}{item view}.
|
---|
40 | Any model derived from QAbstractItemModel can be used as the source of
|
---|
41 | data and almost any input widget can be used to display it.
|
---|
42 |
|
---|
43 | The example itself is very simple: we create \c Window, a QWidget subclass
|
---|
44 | that we use to hold the widgets used to present the data, and show it. The
|
---|
45 | \c Window class will provide buttons that the user can click to show
|
---|
46 | different records from the model.
|
---|
47 |
|
---|
48 | \section1 Window Class Definition
|
---|
49 |
|
---|
50 | The class provides a constructor, a slot to keep the buttons up to date,
|
---|
51 | and a private function to set up the model:
|
---|
52 |
|
---|
53 | \snippet examples/itemviews/simplewidgetmapper/window.h Window definition
|
---|
54 |
|
---|
55 | In addition to the QDataWidgetMapper object and the controls used to make
|
---|
56 | up the user interface, we use a QStandardItemModel to hold our data.
|
---|
57 | We could use a custom model, but this standard implementation is sufficient
|
---|
58 | for our purposes.
|
---|
59 |
|
---|
60 | \section1 Window Class Implementation
|
---|
61 |
|
---|
62 | The constructor of the \c Window class can be explained in three parts.
|
---|
63 | In the first part, we set up the widgets used for the user interface:
|
---|
64 |
|
---|
65 | \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up widgets
|
---|
66 |
|
---|
67 | We also set up the buddy relationships between various labels and the
|
---|
68 | corresponding input widgets.
|
---|
69 |
|
---|
70 | Next, we set up the widget mapper, relating each input widget to a column
|
---|
71 | in the model specified by the call to \l{QDataWidgetMapper::}{setModel()}:
|
---|
72 |
|
---|
73 | \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the mapper
|
---|
74 |
|
---|
75 | We also connect the mapper to the \gui{Next} and \gui{Previous} buttons
|
---|
76 | via its \l{QDataWidgetMapper::}{toNext()} and
|
---|
77 | \l{QDataWidgetMapper::}{toPrevious()} slots. The mapper's
|
---|
78 | \l{QDataWidgetMapper::}{currentIndexChanged()} signal is connected to the
|
---|
79 | \c{updateButtons()} slot in the window which we'll show later.
|
---|
80 |
|
---|
81 | In the final part of the constructor, we set up the layout, placing each
|
---|
82 | of the widgets in a grid (we could also use a QFormLayout for this):
|
---|
83 |
|
---|
84 | \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the layout
|
---|
85 |
|
---|
86 | Lastly, we set the window title and initialize the mapper by setting it to
|
---|
87 | refer to the first row in the model.
|
---|
88 |
|
---|
89 | The model is initialized in the window's \c{setupModel()} function. Here,
|
---|
90 | we create a standard model with 5 rows and 3 columns, and we insert some
|
---|
91 | sample names, addresses and ages into each row:
|
---|
92 |
|
---|
93 | \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the model
|
---|
94 |
|
---|
95 | As a result, each row can be treated like a record in a database, and the
|
---|
96 | widget mapper will read the data from each row, using the column numbers
|
---|
97 | specified earlier to access the correct data for each widget. This is
|
---|
98 | shown in the following diagram:
|
---|
99 |
|
---|
100 | \image widgetmapper-simple-mapping.png
|
---|
101 |
|
---|
102 | Since the user can navigate using the buttons in the user interface, the
|
---|
103 | example is fully-functional at this point, but to make it a bit more
|
---|
104 | user-friendly, we implement the \c{updateButtons()} slot to show when the
|
---|
105 | user is viewing the first or last records:
|
---|
106 |
|
---|
107 | \snippet examples/itemviews/simplewidgetmapper/window.cpp Slot for updating the buttons
|
---|
108 |
|
---|
109 | If the mapper is referring to the first row in the model, the \gui{Previous}
|
---|
110 | button is disabled. Similarly, the \gui{Next} button is disabled if the
|
---|
111 | mapper reaches the last row in the model.
|
---|
112 |
|
---|
113 | \section1 More Complex Mappings
|
---|
114 |
|
---|
115 | The QDataWidgetMapper class makes it easy to relate information from a
|
---|
116 | model to widgets in a user interface. However, it is sometimes necessary
|
---|
117 | to use input widgets which offer choices to the user, such as QComboBox,
|
---|
118 | in conjunction with a widget mapper.
|
---|
119 |
|
---|
120 | In these situations, although the mapping to input widgets remains simple,
|
---|
121 | more work needs to be done to expose additional data to the widget mapper.
|
---|
122 | This is covered by the \l{Combo Widget Mapper Example}{Combo Widget Mapper}
|
---|
123 | and \l{SQL Widget Mapper Example}{SQL Widget Mapper}
|
---|
124 | examples.
|
---|
125 | */
|
---|