[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[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 qt-info@nokia.com.
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example itemviews/combowidgetmapper
|
---|
| 30 | \title Combo Widget Mapper Example
|
---|
| 31 |
|
---|
| 32 | The Delegate Widget Mapper example shows how to use a custom delegate to
|
---|
| 33 | map information from a model to specific widgets on a form.
|
---|
| 34 |
|
---|
| 35 | \image combo-widget-mapper.png
|
---|
| 36 |
|
---|
| 37 | In the \l{Simple Widget Mapper Example}, we showed the basic use of a
|
---|
| 38 | widget mapper to relate data exposed by a model to simple input widgets
|
---|
| 39 | in a user interface. However, sometimes we want to use input widgets that
|
---|
| 40 | expose data as choices to the user, such as QComboBox, and we need a way
|
---|
| 41 | to relate their input to the values stored in the model.
|
---|
| 42 |
|
---|
| 43 | This example is very similar to the \l{Simple Widget Mapper Example}.
|
---|
| 44 | Again, we create a \c Window class with an almost identical user interface,
|
---|
| 45 | except that, instead of providing a spin box so that each person's age
|
---|
| 46 | can be entered, we provide a combo box to allow their addresses to be
|
---|
| 47 | classified as "Home", "Work" or "Other".
|
---|
| 48 |
|
---|
| 49 | \section1 Window Class Definition
|
---|
| 50 |
|
---|
| 51 | The class provides a constructor, a slot to keep the buttons up to date,
|
---|
| 52 | and a private function to set up the model:
|
---|
| 53 |
|
---|
| 54 | \snippet examples/itemviews/combowidgetmapper/window.h Window definition
|
---|
| 55 |
|
---|
| 56 | In addition to the QDataWidgetMapper object and the controls used to make
|
---|
| 57 | up the user interface, we use a QStandardItemModel to hold our data and
|
---|
| 58 | a QStringListModel to hold information about the types of address that
|
---|
| 59 | can be applied to each person's data.
|
---|
| 60 |
|
---|
| 61 | \section1 Window Class Implementation
|
---|
| 62 |
|
---|
| 63 | The constructor of the \c Window class can be explained in three parts.
|
---|
| 64 | In the first part, we set up the widgets used for the user interface:
|
---|
| 65 |
|
---|
| 66 | \snippet examples/itemviews/combowidgetmapper/window.cpp Set up widgets
|
---|
| 67 |
|
---|
| 68 | Note that we set up the mapping the combo box in the same way as for other
|
---|
| 69 | widgets, but that we apply its own model to it so that it will display
|
---|
| 70 | data from its own model, the \c typeModel, rather than from the model
|
---|
| 71 | containing data about each person.
|
---|
| 72 |
|
---|
| 73 | Next, we set up the widget mapper, relating each input widget to a column
|
---|
| 74 | in the model specified by the call to \l{QDataWidgetMapper::}{setModel()}:
|
---|
| 75 |
|
---|
| 76 | \snippet examples/itemviews/combowidgetmapper/window.cpp Set up the mapper
|
---|
| 77 |
|
---|
| 78 | For the combo box, we pass an extra argument to tell the widget mapper
|
---|
| 79 | which property to relate to values from the model. As a result, the user
|
---|
| 80 | is able to select an item from the combo box, and the corresponding
|
---|
| 81 | value stored in the widget's \c currentIndex property will be stored in
|
---|
| 82 | the model.
|
---|
| 83 |
|
---|
| 84 | \omit
|
---|
| 85 | However, we also set a delegate on the mapper. As with \l{Delegate Classes},
|
---|
| 86 | this changes the way that data is presented to the user. In this case, the
|
---|
| 87 | delegate acts as a proxy between the mapper and the input widgets,
|
---|
| 88 | translating the data into a suitable form for the combo box but not
|
---|
| 89 | interfering with the other input widgets. The implementation is shown later.
|
---|
| 90 | \endomit
|
---|
| 91 |
|
---|
| 92 | The rest of the constructor is very similar to that of the
|
---|
| 93 | \l{Simple Widget Mapper Example}:
|
---|
| 94 |
|
---|
| 95 | \snippet examples/itemviews/combowidgetmapper/window.cpp Set up connections and layouts
|
---|
| 96 |
|
---|
| 97 | The model is initialized in the window's \c{setupModel()} function. Here,
|
---|
| 98 | we create a standard model with 5 rows and 3 columns. In each row, we
|
---|
| 99 | insert a name, address, and a value that indicates the type of address.
|
---|
| 100 | The address types are stored in a string list model.
|
---|
| 101 |
|
---|
| 102 | \snippet examples/itemviews/combowidgetmapper/window.cpp Set up the model
|
---|
| 103 |
|
---|
| 104 | As we insert each row into the model, like a record in a database, we
|
---|
| 105 | store values that correspond to items in \c typeModel for each person's
|
---|
| 106 | address type. When the widget mapper reads these values from the final
|
---|
| 107 | column of each row, it will need to use them as references to values in
|
---|
| 108 | \c typeModel, as shown in the following diagram. This is where the
|
---|
| 109 | delegate is used.
|
---|
| 110 |
|
---|
| 111 | \image widgetmapper-combo-mapping.png
|
---|
| 112 |
|
---|
| 113 | We show the implementation of the \c{updateButtons()} slot for
|
---|
| 114 | completeness:
|
---|
| 115 |
|
---|
| 116 | \snippet examples/itemviews/combowidgetmapper/window.cpp Slot for updating the buttons
|
---|
| 117 |
|
---|
| 118 | \omit
|
---|
| 119 | \section1 Delegate Class Definition and Implementation
|
---|
| 120 |
|
---|
| 121 | The delegate we use to mediate interaction between the widget mapper and
|
---|
| 122 | the input widgets is a small QItemDelegate subclass:
|
---|
| 123 |
|
---|
| 124 | \snippet examples/itemviews/combowidgetmapper/delegate.h Delegate class definition
|
---|
| 125 |
|
---|
| 126 | This provides implementations of the two standard functions used to pass
|
---|
| 127 | data between editor widgets and the model (see the \l{Delegate Classes}
|
---|
| 128 | documentation for a more general description of these functions).
|
---|
| 129 |
|
---|
| 130 | Since we only provide an empty implementation of the constructor, we
|
---|
| 131 | concentrate on the other two functions.
|
---|
| 132 |
|
---|
| 133 | The \l{QItemDelegate::}{setEditorData()} implementation takes the data
|
---|
| 134 | referred to by the model index supplied and processes it according to
|
---|
| 135 | the presence of a \c currentIndex property in the editor widget:
|
---|
| 136 |
|
---|
| 137 | \snippet examples/itemviews/combowidgetmapper/delegate.cpp setEditorData implementation
|
---|
| 138 |
|
---|
| 139 | If, like QComboBox, the editor widget has this property, it is set using
|
---|
| 140 | the value from the model. Since we are passing around QVariant values,
|
---|
| 141 | the strings stored in the model are automatically converted to the integer
|
---|
| 142 | values needed for the \c currentIndex property.
|
---|
| 143 |
|
---|
| 144 | As a result, instead of showing "0", "1" or "2" in the combo box, one of
|
---|
| 145 | its predefined set of items is shown. We call QItemDelegate::setEditorData()
|
---|
| 146 | for widgets without the \c currentIndex property.
|
---|
| 147 |
|
---|
| 148 | The \l{QItemDelegate::}{setModelData()} implementation performs the reverse
|
---|
| 149 | process, taking the value stored in the widget's \c currentIndex property
|
---|
| 150 | and storing it back in the model:
|
---|
| 151 |
|
---|
| 152 | \snippet examples/itemviews/combowidgetmapper/delegate.cpp setModelData implementation
|
---|
| 153 | \endomit
|
---|
| 154 |
|
---|
| 155 | \section1 Summary and Further Reading
|
---|
| 156 |
|
---|
| 157 | The use of a separate model for the combo box provides a menu of choices
|
---|
| 158 | that are separate from the data stored in the main model. Using a named
|
---|
| 159 | mapping that relates the combo box's \c currentIndex property to a column
|
---|
| 160 | in the model effectively allows us to store a look-up value in the model.
|
---|
| 161 |
|
---|
| 162 | However, when reading the model outside the context of the widget mapper,
|
---|
| 163 | we need to know about the \c typeModel to make sense of these look-up
|
---|
| 164 | values. It would be useful to be able to store both the data and the
|
---|
| 165 | choices held by the \c typeModel in one place.
|
---|
| 166 | This is covered by the \l{SQL Widget Mapper Example}.
|
---|
| 167 | */
|
---|