summaryrefslogtreecommitdiffstats
path: root/doc/src/examples/spinboxdelegate.qdoc
diff options
context:
space:
mode:
authorQt by Nokia <qt-info@nokia.com>2011-04-27 12:05:43 +0200
committeraxis <qt-info@nokia.com>2011-04-27 12:05:43 +0200
commit38be0d13830efd2d98281c645c3a60afe05ffece (patch)
tree6ea73f3ec77f7d153333779883e8120f82820abe /doc/src/examples/spinboxdelegate.qdoc
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'doc/src/examples/spinboxdelegate.qdoc')
-rw-r--r--doc/src/examples/spinboxdelegate.qdoc137
1 files changed, 137 insertions, 0 deletions
diff --git a/doc/src/examples/spinboxdelegate.qdoc b/doc/src/examples/spinboxdelegate.qdoc
new file mode 100644
index 0000000000..bb62dd277e
--- /dev/null
+++ b/doc/src/examples/spinboxdelegate.qdoc
@@ -0,0 +1,137 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Free Documentation License
+** 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.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \example itemviews/spinboxdelegate
+ \title Spin Box Delegate Example
+
+ The Spin Box Delegate example shows how to create an editor for a custom delegate in
+ the model/view framework by reusing a standard Qt editor widget.
+
+ The model/view framework provides a standard delegate that is used by default
+ with the standard view classes. For most purposes, the selection of editor
+ widgets available through this delegate is sufficient for editing text, boolean
+ values, and other simple data types. However, for specific data types, it is
+ sometimes necessary to use a custom delegate to either display the data in a
+ specific way, or allow the user to edit it with a custom control.
+
+ \image spinboxdelegate-example.png
+
+ This concepts behind this example are covered in the
+ \l{Model/View Programming#Delegate Classes}{Delegate Classes} chapter
+ of the \l{Model/View Programming} overview.
+
+ \section1 SpinBoxDelegate Class Definition
+
+ The definition of the delegate is as follows:
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.h 0
+
+ The delegate class declares only those functions that are needed to
+ create an editor widget, display it at the correct location in a view,
+ and communicate with a model. Custom delegates can also provide their
+ own painting code by reimplementing the \c paintEvent() function.
+
+ \section1 SpinBoxDelegate Class Implementation
+
+ Since the delegate is stateless, the constructor only needs to
+ call the base class's constructor with the parent QObject as its
+ argument:
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.cpp 0
+
+ Since the delegate is a subclass of QItemDelegate, the data it retrieves
+ from the model is displayed in a default style, and we do not need to
+ provide a custom \c paintEvent().
+
+ The \c createEditor() function returns an editor widget, in this case a
+ spin box that restricts values from the model to integers from 0 to 100
+ inclusive.
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.cpp 1
+
+ We install an event filter on the spin box to ensure that it behaves in
+ a way that is consistent with other delegates. The implementation for
+ the event filter is provided by the base class.
+
+ The \c setEditorData() function reads data from the model, converts it
+ to an integer value, and writes it to the editor widget.
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.cpp 2
+
+ Since the view treats delegates as ordinary QWidget instances, we have
+ to use a static cast before we can set the value in the spin box.
+
+ The \c setModelData() function reads the contents of the spin box, and
+ writes it to the model.
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.cpp 3
+
+ We call \l{QSpinBox::interpretText()}{interpretText()} to make sure that
+ we obtain the most up-to-date value in the spin box.
+
+ The \c updateEditorGeometry() function updates the editor widget's
+ geometry using the information supplied in the style option. This is the
+ minimum that the delegate must do in this case.
+
+ \snippet examples/itemviews/spinboxdelegate/delegate.cpp 4
+
+ More complex editor widgets may divide the rectangle available in
+ \c{option.rect} between different child widgets if required.
+
+ \section1 The Main Function
+
+ This example is written in a slightly different way to many of the
+ other examples supplied with Qt. To demonstrate the use of a custom
+ editor widget in a standard view, it is necessary to set up a model
+ containing some arbitrary data and a view to display it.
+
+ We set up the application in the normal way, construct a standard item
+ model to hold some data, set up a table view to use the data in the
+ model, and construct a custom delegate to use for editing:
+
+ \snippet examples/itemviews/spinboxdelegate/main.cpp 0
+
+ The table view is informed about the delegate, and will use it to
+ display each of the items. Since the delegate is a subclass of
+ QItemDelegate, each cell in the table will be rendered using standard
+ painting operations.
+
+ We insert some arbitrary data into the model for demonstration purposes:
+
+ \snippet examples/itemviews/spinboxdelegate/main.cpp 1
+ \snippet examples/itemviews/spinboxdelegate/main.cpp 2
+
+ Finally, the table view is displayed with a window title, and we start
+ the application's event loop:
+
+ \snippet examples/itemviews/spinboxdelegate/main.cpp 3
+
+ Each of the cells in the table can now be edited in the usual way, but
+ the spin box ensures that the data returned to the model is always
+ constrained by the values allowed by the spin box delegate.
+*/