doc/src/examples/simplewidgetmapper.qdoc
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*!
       
    43     \example itemviews/simplewidgetmapper
       
    44     \title Simple Widget Mapper Example
       
    45 
       
    46     The Simple Widget Mapper example shows how to use a widget mapper to display
       
    47     data from a model in a collection of widgets.
       
    48 
       
    49     \image simplewidgetmapper-example.png
       
    50 
       
    51     The QDataWidgetMapper class allows information obtained from a
       
    52     \l{Model Classes}{model} to be viewed and edited in a collection of
       
    53     widgets instead of in an \l{View Classes}{item view}.
       
    54     Any model derived from QAbstractItemModel can be used as the source of
       
    55     data and almost any input widget can be used to display it.
       
    56 
       
    57     The example itself is very simple: we create \c Window, a QWidget subclass
       
    58     that we use to hold the widgets used to present the data, and show it. The
       
    59     \c Window class will provide buttons that the user can click to show
       
    60     different records from the model.
       
    61 
       
    62     \section1 Window Class Definition
       
    63 
       
    64     The class provides a constructor, a slot to keep the buttons up to date,
       
    65     and a private function to set up the model:
       
    66 
       
    67     \snippet examples/itemviews/simplewidgetmapper/window.h Window definition
       
    68 
       
    69     In addition to the QDataWidgetMapper object and the controls used to make
       
    70     up the user interface, we use a QStandardItemModel to hold our data.
       
    71     We could use a custom model, but this standard implementation is sufficient
       
    72     for our purposes.
       
    73 
       
    74     \section1 Window Class Implementation
       
    75 
       
    76     The constructor of the \c Window class can be explained in three parts.
       
    77     In the first part, we set up the widgets used for the user interface:
       
    78     
       
    79     \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up widgets
       
    80 
       
    81     We also set up the buddy relationships between various labels and the
       
    82     corresponding input widgets.
       
    83 
       
    84     Next, we set up the widget mapper, relating each input widget to a column
       
    85     in the model specified by the call to \l{QDataWidgetMapper::}{setModel()}:
       
    86 
       
    87     \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the mapper
       
    88 
       
    89     We also connect the mapper to the \gui{Next} and \gui{Previous} buttons
       
    90     via its \l{QDataWidgetMapper::}{toNext()} and
       
    91     \l{QDataWidgetMapper::}{toPrevious()} slots. The mapper's
       
    92     \l{QDataWidgetMapper::}{currentIndexChanged()} signal is connected to the
       
    93     \c{updateButtons()} slot in the window which we'll show later.
       
    94 
       
    95     In the final part of the constructor, we set up the layout, placing each
       
    96     of the widgets in a grid (we could also use a QFormLayout for this):
       
    97 
       
    98     \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the layout
       
    99 
       
   100     Lastly, we set the window title and initialize the mapper by setting it to
       
   101     refer to the first row in the model.
       
   102 
       
   103     The model is initialized in the window's \c{setupModel()} function. Here,
       
   104     we create a standard model with 5 rows and 3 columns, and we insert some
       
   105     sample names, addresses and ages into each row:
       
   106 
       
   107     \snippet examples/itemviews/simplewidgetmapper/window.cpp Set up the model
       
   108 
       
   109     As a result, each row can be treated like a record in a database, and the
       
   110     widget mapper will read the data from each row, using the column numbers
       
   111     specified earlier to access the correct data for each widget. This is
       
   112     shown in the following diagram:
       
   113 
       
   114     \image widgetmapper-simple-mapping.png
       
   115 
       
   116     Since the user can navigate using the buttons in the user interface, the
       
   117     example is fully-functional at this point, but to make it a bit more
       
   118     user-friendly, we implement the \c{updateButtons()} slot to show when the
       
   119     user is viewing the first or last records:
       
   120 
       
   121     \snippet examples/itemviews/simplewidgetmapper/window.cpp Slot for updating the buttons
       
   122 
       
   123     If the mapper is referring to the first row in the model, the \gui{Previous}
       
   124     button is disabled. Similarly, the \gui{Next} button is disabled if the
       
   125     mapper reaches the last row in the model.
       
   126 
       
   127     \section1 More Complex Mappings
       
   128 
       
   129     The QDataWidgetMapper class makes it easy to relate information from a
       
   130     model to widgets in a user interface. However, it is sometimes necessary
       
   131     to use input widgets which offer choices to the user, such as QComboBox,
       
   132     in conjunction with a widget mapper.
       
   133 
       
   134     In these situations, although the mapping to input widgets remains simple,
       
   135     more work needs to be done to expose additional data to the widget mapper.
       
   136     This is covered by the \l{Combo Widget Mapper Example}{Combo Widget Mapper}
       
   137     and \l{SQL Widget Mapper Example}{SQL Widget Mapper}
       
   138     examples.
       
   139 */