diff options
Diffstat (limited to 'src/widgets/doc/src/model-view-programming.qdoc')
-rw-r--r-- | src/widgets/doc/src/model-view-programming.qdoc | 73 |
1 files changed, 23 insertions, 50 deletions
diff --git a/src/widgets/doc/src/model-view-programming.qdoc b/src/widgets/doc/src/model-view-programming.qdoc index ede1ebf932..5461619a4d 100644 --- a/src/widgets/doc/src/model-view-programming.qdoc +++ b/src/widgets/doc/src/model-view-programming.qdoc @@ -1,29 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only /*! \page model-view-programming.html \ingroup qt-basic-concepts @@ -473,11 +449,13 @@ \snippet simplemodel-use/main.cpp 0 In this case, we start by setting up a default QFileSystemModel. We connect - it to a lambda, in which we will obtain a parent index using a specific + its signal \c directoryLoaded(QString) to a lambda, in which we will + obtain a parent index for the directory using a specific implementation of \l{QFileSystemModel::}{index()} provided by that model. - In the lambda, we count the number of rows in the model using the - \l{QFileSystemModel::}{rowCount()} function. Finally, we set the root path - of the QFileSystemModel so it starts loading data and triggers the lambda. + + In the lambda, we determine the number of rows in the model using the + \l{QFileSystemModel::}{rowCount()} function. + For simplicity, we are only interested in the items in the first column of the model. We examine each row in turn, obtaining a model index for @@ -498,6 +476,9 @@ \codeline \snippet simplemodel-use/main.cpp 3 + Finally, we set the root path of the QFileSystemModel so it starts + loading data and triggers the lambda. + The above example demonstrates the basic principles used to retrieve data from a model: @@ -705,12 +686,8 @@ implementations of these functions. Editors for delegates can be implemented either by using widgets to manage - the editing process or by handling events directly. - The first approach is covered later in this section, and it is also - shown in the \l{Spin Box Delegate Example}{Spin Box Delegate} example. - - The \l{Pixelator Example}{Pixelator} example shows how to create a - custom delegate that performs specialized rendering for a table view. + the editing process or by handling events directly. The first approach is + covered later in this section. \section2 Using an existing delegate @@ -740,13 +717,15 @@ data entry. We construct a table view to display the contents of the model, and this will use the custom delegate for editing. - \image spinboxdelegate-example.png + \image spinboxdelegate-example.webp We subclass the delegate from \l QStyledItemDelegate because we do not want to write custom display functions. However, we must still provide functions to manage the editor widget: - \snippet itemviews/spinboxdelegate/delegate.h 0 + \snippet qitemdelegate/spinbox-delegate.cpp declaration + \codeline + \snippet qitemdelegate/spinbox-delegate.cpp constructor Note that no editor widgets are set up when the delegate is constructed. We only construct an editor widget when it is needed. @@ -760,7 +739,7 @@ supplied with everything that the delegate needs to be able to set up a suitable widget: - \snippet itemviews/spinboxdelegate/delegate.cpp 1 + \snippet qitemdelegate/spinbox-delegate.cpp createEditor Note that we do not need to keep a pointer to the editor widget because the view takes responsibility for destroying it when it is no longer @@ -784,7 +763,7 @@ \l{Qt::ItemDataRole}{display role}, and set the value in the spin box accordingly. - \snippet itemviews/spinboxdelegate/delegate.cpp 2 + \snippet qitemdelegate/spinbox-delegate.cpp setEditorData In this example, we know that the editor widget is a spin box, but we could have provided different editors for different types of data in @@ -797,7 +776,7 @@ asks the delegate to store the edited value in the model by calling the \l{QAbstractItemDelegate::setModelData()}{setModelData()} function. - \snippet itemviews/spinboxdelegate/delegate.cpp 3 + \snippet qitemdelegate/spinbox-delegate.cpp setModelData Since the view manages the editor widgets for the delegate, we only need to update the model with the contents of the editor supplied. @@ -808,8 +787,8 @@ finished editing by emitting the \l{QAbstractItemDelegate::closeEditor()}{closeEditor()} signal. The view ensures that the editor widget is closed and destroyed. In - this example, we only provide simple editing facilities, so we need - never emit this signal. + this example, we only provide simple editing facilities, so we never + need to emit this signal. All the operations on data are performed through the interface provided by \l QAbstractItemModel. This makes the delegate mostly @@ -828,7 +807,7 @@ the view provides all the necessary geometry information inside a \l{QStyleOptionViewItem}{view option} object. - \snippet itemviews/spinboxdelegate/delegate.cpp 4 + \snippet qitemdelegate/spinbox-delegate.cpp updateEditorGeometry In this case, we just use the geometry information provided by the view option in the item rectangle. A delegate that renders items with @@ -1837,8 +1816,6 @@ Note that the model will typically need to provide implementations of the QAbstractItemModel::insertRows() and QAbstractItemModel::setData() functions. - \sa {itemviews/puzzle}{Item Views Puzzle Example} - \section1 Proxy Models In the model/view framework, items of data supplied by a single model can be shared @@ -2332,10 +2309,6 @@ \section1 Related Examples \list - \li \l{itemviews/dirview}{Dir View} - \li \l{itemviews/spinboxdelegate}{Spin Box Delegate} - \li \l{itemviews/pixelator}{Pixelator} \li \l{itemviews/simpletreemodel}{Simple Tree Model} - \li \l{itemviews/chart}{Chart} \endlist */ |