summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xdoc/src/tutorials/modelview.qdoc791
-rwxr-xr-xexamples/tutorials/modelview/1_readonly/main.cpp1
-rwxr-xr-xexamples/tutorials/modelview/1_readonly/modelview.cpp5
-rwxr-xr-xexamples/tutorials/modelview/1_readonly/modelview.h6
-rwxr-xr-xexamples/tutorials/modelview/1_readonly/mymodel.cpp14
-rwxr-xr-xexamples/tutorials/modelview/1_readonly/mymodel.h5
-rwxr-xr-xexamples/tutorials/modelview/2_formatting/modelview.cpp2
-rwxr-xr-xexamples/tutorials/modelview/2_formatting/mymodel.cpp22
-rwxr-xr-xexamples/tutorials/modelview/3_changingmodel/modelview.cpp2
-rwxr-xr-xexamples/tutorials/modelview/3_changingmodel/mymodel.cpp16
-rwxr-xr-xexamples/tutorials/modelview/4_headers/modelview.cpp2
-rwxr-xr-xexamples/tutorials/modelview/4_headers/mymodel.cpp10
-rwxr-xr-xexamples/tutorials/modelview/5_edit/modelview.cpp6
-rwxr-xr-xexamples/tutorials/modelview/5_edit/mymodel.cpp21
-rwxr-xr-xexamples/tutorials/modelview/5_edit/mymodel.h9
-rwxr-xr-xexamples/tutorials/modelview/6_treeview/modelview.cpp8
-rwxr-xr-xexamples/tutorials/modelview/6_treeview/modelview.h2
-rwxr-xr-xexamples/tutorials/modelview/7_selections/modelview.cpp8
-rwxr-xr-xexamples/tutorials/modelview/qmake.pro10
19 files changed, 564 insertions, 376 deletions
diff --git a/doc/src/tutorials/modelview.qdoc b/doc/src/tutorials/modelview.qdoc
index bc03f44..98096a0 100755
--- a/doc/src/tutorials/modelview.qdoc
+++ b/doc/src/tutorials/modelview.qdoc
@@ -1,10 +1,37 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 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$
+** Commercial Usage
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in a
+** written agreement between you and Nokia.
+**
+** 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$
+**
+****************************************************************************/
+
/*!
\page modelview.html
\startpage {index.html}{Qt Reference Documentation}
- \contentspage {modelview-index.html}{ModelView Contents}
\nextpage {modelview-part1.html}{Introduction}
- \title ModelView Contents Page
+
+ \title Model/View Contents
\brief An introduction to ModelView programming
This tutorial gives an introduction to ModelView programming using the Qt
@@ -14,17 +41,17 @@
\omit
It doesn't cover everything; the emphasis is on teaching the programming
- philosophy of ModelView programming, and Qt's features are introduced as needed.
- Some commonly used features are never used in this tutorial.
+ philosophy of Model/View programming, and Qt's features are introduced as
+ needed. Some commonly used features are never used in this tutorial.
\endomit
In the process, we will learn about some basic technologies provided by Qt,
such as:
\list
- \o The difference between standard and modelview widgets
+ \o The difference between standard and model/view widgets
\o Adapters betweeen forms and models
- \o Developing a simple Model/View application
+ \o Developing a simple model/view application
\o Intermediate topics such as:
\list
\o Tree views
@@ -43,9 +70,9 @@
\list 1
\o \l{modelview-part1.html}{Introduction}
- \o \l{modelview-part2.html}{Developing a simple ModelView application}
+ \o \l{modelview-part2.html}{Developing a Simple Model/View Application}
\o \l{modelview-part3.html}{Intermediate Topics}
- \o \l{modelview-part4.html}{Good Sources for Additional Information}
+ \o \l{modelview-part4.html}{Good Sources of Additional Information}
\endlist
@@ -53,13 +80,18 @@
/*!
\page modelview-part1.html
+ \contentspage {modelview-index.html}{Model/View Contents}
+ \previouspage {modelview-index.html}{Model/View Contents}
+ \nextpage {modelview-part2.html}{Developing a Simple Model/View Application}
\title An Introduction to Model/View Programming
- \raw HTML
- <br>
- \endraw
-\section1 1 Introduction
-Model/View is a technology used to separate data from views in widgets that handle data sets. Standard Widgets are not designed for separating data from views and this is why Qt 4 has two different types of widgets. Both types of widgets look the same, but they interact with data differently.
+ \section1 1. Introduction
+
+ Model/View is a technology used to separate data from views in widgets that
+ handle data sets. Standard widgets are not designed for separating data
+ from views and this is why Qt 4 has two different types of widgets. Both
+ types of widgets look the same, but they interact with data differently.
+
\table
\row
\o Standard widgets use data that is part of the widget.
@@ -68,129 +100,193 @@ Model/View is a technology used to separate data from views in widgets that hand
\o View classes operate on external data (the model)
\o \image modelview.png
\endtable
-\section2 1.1 Standard widgets
-Let's have a closer look at a standard table widget. A table widget is a 2D array of the data elements that the user can change. The table widget can be integrated into a program flow by reading and writing the data elements that the table widget provides. This method is very intuitive and useful in many applications.
-Displaying and editing a database table with a standard table widget can be problematic. Two copies of the data have to be coordinated: one outside the widget; one inside the widget. The developer needs to know where up-to-date data is so the both copies contain the most recent data. The tight coupling of presentation and data makes it harder to write unit tests.
+ \section2 1.1 Standard Widgets
+
+ Let's have a closer look at a standard table widget. A table widget is a 2D
+ array of the data elements that the user can change. The table widget can
+ be integrated into a program flow by reading and writing the data elements
+ that the table widget provides. This method is very intuitive and useful in
+ many applications.
+
+ Displaying and editing a database table with a standard table widget can be
+ problematic. Two copies of the data have to be coordinated: one outside the
+ widget; one inside the widget. The developer needs to know where up-to-date
+ data is so the both copies contain the most recent data. The tight coupling
+ of presentation and data makes it harder to write unit tests.
+
+ \section2 1.2 Model/View to the Rescue
+
+ Model/view stepped up to provide a solution that uses a more versatile
+ architecture. Model/view eliminates the data consistency problems that may
+ occur with standard widgets. Model/view also makes it easier to use more
+ than one view of the same data because one model can be passed on to many
+ views. The most important difference is that model/view widgets do not
+ store data behind the table cells. In fact, they operate directly from your
+ data. Since view classes do not know your data's structure, you need to
+ provide a wrapper to make your data conform to the QAbstractItemModel
+ interface. A view uses this interface to read from and write to your data
+ and any class that implements QAbstractItemModel is a model. Once the view
+ receives a pointer to a model, it will read and display its content and be
+ its editor.
+
+ \section2 1.3 Overview of the Model/View Widgets
-\section2 1.2 Model/View to the rescue
-Model/View stepped up to provide a solution that uses a more versatile architecture. Model/View eliminates the data consistency problems that may occur with standard widgets. Model/View also makes it easier to use more than one view of the same data because one model can be passed on to many views. The most important difference is that model/view widgets do not store data behind the table cells. In fact, they operate directly from your data. Since view classes do not know your data's structure, you need to provide a wrapper to make your data conform to the \l QAbstractItemModel interface. A view uses this interface to read from and write to your data and any class that implements \l QAbstractItemModel is a model. Once the view receives a pointer to a model, it will read and display its content and be its editor.
+ Here is an overview of the model/view widgets and their corresponding
+ standard widgets.
-\section2 1.3 Overview of the model/view widgets
-Here is an overview of the model/view widgets and their corresponding standard widgets.
\table
\header
\o Widget
- \o Standard Widget
-(a convenience class with data in the widget)
+ \o Standard Widget (a convenience class with data in
+ the widget)
\o Model/View View Class (for use with external data)
\row
- \o \image listview.png
+ \o \inlineimage listview.png
\o \l QListWidget
\o \l QListView
\row
- \o \image tableview.png
+ \o \inlineimage tableview.png
\o \l QTableWidget
\o \l QTableView
\row
- \o \image treeview.png
+ \o \inlineimage treeview.png
\o \l QTreeWidget
\o \l QTreeView
\row
- \o \image columnview.png
- \o
+ \o \inlineimage columnview.png
+ \o
\o \l QColumnView shows a tree as a hierarchy of lists
\row
- \o \image combobox.png
- \o {2, 1} \l QComboBox can work as both a view class and also as a traditional widget
+ \o \inlineimage combobox.png
+ \o {2, 1} \l QComboBox can work as both a view class and also
+ as a traditional widget
\endtable
-\section2 1.4 Having adapters between forms and models can come in handy.
-We often prefer editing data stored in tables (e.g. in database tables) in forms rather than in tables. There is no direct model/view counterpart for separating data and views for widgets that operate on one value instead of a dataset, so we need an adapter in order to connect the form to the source of data.
+ \section2 1.4 Using Adapters between Forms and Models
-\l QDataWidgetMapper is a great solution because it maps form widgets to a table row and it makes it very easy to build forms for database tables.
-\image widgetmapper.png
+ Having adapters between forms and models can come in handy.
+ We often prefer editing data stored in tables (e.g. in database tables) in
+ forms rather than in tables. There is no direct model/view counterpart for
+ separating data and views for widgets that operate on one value instead of
+ a dataset, so we need an adapter in order to connect the form to the source
+ of data.
-Another example of an adapter is \l QCompleter. Qt has QCompleter for providing auto completions in Qt widgets such as \l QComboBox and, as shown below, \l QLineEdit. \l QCompleter uses a model as its data source, so \l QCompleter, in itself, is a very handy adapter.
-\image qcompleter.png
+ \l QDataWidgetMapper is a great solution because it maps form widgets to a
+ table row and it makes it very easy to build forms for database tables.
+ \image widgetmapper.png
+ Another example of an adapter is QCompleter. Qt has QCompleter for
+ providing auto-completions in Qt widgets such as QComboBox and, as shown
+ below, QLineEdit. QCompleter uses a model as its data source, so QCompleter,
+ in itself, is a very handy adapter.
+
+ \image qcompleter.png
*/
/*!
-
-
\page modelview-part2-main-cpp.html
\title main.cpp
\quotefile tutorials/modelview/1_readonly/main.cpp
-
-
*/
/*!
\page modelview-part2.html
- \title ModelView Chapter 2 - A Simple Model/View Application
-
- \raw HTML
- <br>
- \endraw
-
-\section1 2 A Simple Model/View Application
-If you want to develop a model/view application, where should you start? We recommend starting with a simple example and extending it step-by-step. This makes understanding the architecture a lot easier. Trying to understand the model/view architecture in detail before invoking the IDE has proven to be less convenient for many developers. It is substantially easier to start with a simple model/view application that has demo data. Give it a try! Simply replace the data in the examples below with your own.
+ \contentspage {modelview-index.html}{Model/View Contents}
+ \previouspage {modelview-part1.html}{Introduction}
+ \nextpage {modelview-part3.html}{Intermediate Topics}
+ \title Model/View Chapter 2 - A Simple Model/View Application
+
+ \section1 2. A Simple Model/View Application
+
+ If you want to develop a model/view application, where should you start? We
+ recommend starting with a simple example and extending it step-by-step.
+ This makes understanding the architecture a lot easier. Trying to
+ understand the model/view architecture in detail before invoking the IDE
+ has proven to be less convenient for many developers. It is substantially
+ easier to start with a simple model/view application that has demo data.
+ Give it a try! Simply replace the data in the examples below with your own.
+
+ Below are 7 very simple and independent applications that show different
+ sides of model/view programming. The source code can be found inside the
+ \c{examples/tutorials/modelview} directory.
-Below are 7 very simple and independent applications that show different sides of model/view programming. The source code can be downloaded from @todo___________paste link here_________________________
+ \section2 2.1 A Read Only Table
-\section2 2.1 A read only table
-We start with an application that uses a \l QTableView to show data. We will add editing capabilities later.
+ We start with an application that uses a QTableView to show data. We will
+ add editing capabilities later.
--------------------------------------------------------------main.cpp---------------------
\snippet examples/tutorials/modelview/1_readonly/main.cpp Quoting ModelView Tutorial
-We have the usual \l {modelview-part2-main-cpp.html}{main()} function;
--------------------------------------------------------------modelview.h---------------------
+ We have the usual \l {modelview-part2-main-cpp.html}{main()} function:
+
\snippet examples/tutorials/modelview/1_readonly/modelview.h Quoting ModelView Tutorial
-The application is a \l QMainWindow that holds a \l QTableView.
+ The application is a \l QMainWindow that holds a \l QTableView.
--------------------------------------------------------------modelview.cpp---------------------
\snippet examples/tutorials/modelview/1_readonly/modelview.cpp Quoting ModelView Tutorial
-Here is the interesting part: We use \l{QTableView::setModel()}{tableView->setModel(new MyModel(this) );} to instantiate the Model and pass its pointer to \l {QTableView}{tableView()}. \l {QTableView}{tableView} will invoke the methods of the pointer it has received to find out two things:
+ Here is the interesting part: We use
+ \l{QTableView::setModel()}{tableView->setModel(new MyModel(this));} to
+ instantiate the Model and pass its pointer to \l {QTableView}{tableView()}.
+ \l{QTableView}{tableView} will invoke the methods of the pointer it has
+ received to find out two things:
+
\list
\o How many rows and columns should be displayed
\o What content should be printed into each cell.
\endlist
-The model needs some code to respond to this.
+ The model needs some code to respond to this.
-We have a table data set, so let's start with QAbstractTableModel since it is easier to use.
--------------------------------------------------------------mymodel.h---------------------
- \snippet examples/tutorials/modelview/1_readonly/mymodel.h Quoting ModelView Tutorial
+ We have a table data set, so let's start with QAbstractTableModel since it
+ is easier to use.
-QAbstractTableModel requires the implementation of three abstract methods.
+ \snippet examples/tutorials/modelview/1_readonly/mymodel.h Quoting ModelView Tutorial
+ QAbstractTableModel requires the implementation of three abstract methods.
--------------------------------------------------------------mymodel.cpp---------------------
\snippet examples/tutorials/modelview/1_readonly/mymodel.cpp Quoting ModelView Tutorial
-The number of rows and columns is set by \l{QAbstractItemModel::rowCount()}{MyModel::rowCount()} and \l{QAbstractItemModel::columnCount()}{MyModel::columnCount()}.
-When the view has to know what the cell 's text is, it calls the method. Row and column information is specified with parameter \c index and the role is set to \l{Qt::ItemDataRole}{Qt::DisplayRole}. Other roles are covered in the next section. In our example, the data that should be displayed is generated. In a real application, \c MyModel would have a member called \c MyData, which serves as the target for all reading and writing operations.
-
-This small example demonstrates the passive nature of a model. The model does not know when it will be used or which data is needed. It simply provides data each time the view requests it.
-
-What happens when the model 's data needs to be changed? How does the view know when data changes and needs to be read again? The model has to emit a signal that indicates what range of cells has changed. This will be demonstrated in section 2.3.
+ The number of rows and columns is set by
+ \l{QAbstractItemModel::rowCount()}{MyModel::rowCount()} and
+ \l{QAbstractItemModel::columnCount()}{MyModel::columnCount()}.
+ When the view has to know what the cell's text is, it calls the method.
+ Row and column information is specified with parameter \c index and the
+ role is set to \l{Qt::ItemDataRole}{Qt::DisplayRole}. Other roles are
+ covered in the next section. In our example, the data that should be
+ displayed is generated. In a real application, \c MyModel would have a
+ member called \c MyData, which serves as the target for all reading and
+ writing operations.
+
+ This small example demonstrates the passive nature of a model. The model
+ does not know when it will be used or which data is needed. It simply
+ provides data each time the view requests it.
+
+ What happens when the model 's data needs to be changed? How does the view
+ know when data changes and needs to be read again? The model has to emit a
+ signal that indicates what range of cells has changed. This will be
+ demonstrated in section 2.3.
+
+ \section2 2.2 Extending the Read Only Example with Roles
+
+ In addition to controlling what text the view displays, the model also
+ controls the text's appearance. When we slightly change the model, we get
+ the following result: \image readonlytable_role.png
+
+ In fact, nothing except for the \l{QAbstractItemModel::}{data()}
+ method needs to be changed to set fonts, background colour, alignment and a
+ checkbox.
+ Here is the \l{QAbstractItemModel::data()}{data()} method that produces the
+ result shown above:
-\section2 2.2 Extending the read only example with roles
-In addition to controlling what text the view displays, the model also controls the text's appearance. When we slightly change the model, we get the following result: \image readonlytable_role.png
-
-
-In fact, nothing except for the \l{QAbstractItemModel::data()}{data()} method needs to be changed to set fonts, background colour, alignment and a checkbox. Here is the \l{QAbstractItemModel::data()}{data()} method that produces the result shown above:
-
--------------------------------------------------------------mymodel.cpp---------------------
\snippet examples/tutorials/modelview/2_formatting/mymodel.cpp Quoting ModelView Tutorial
-Each formatting property will be requested from the model with a separate call to the \l{QAbstractItemModel::data()}{data()} method. The \c role parameter is used to let the model know which property is being requested:
+ Each formatting property will be requested from the model with a separate
+ call to the \l{QAbstractItemModel::data()}{data()} method. The \c role
+ parameter is used to let the model know which property is being requested:
\table
\header
@@ -214,114 +310,194 @@ Each formatting property will be requested from the model with a separate call t
\o text alignment
\o enum Qt::AlignmentFlag
\row
- \o {1, 3} Qt::CheckStateRole
- \o {1, 3} suppresses checkboxes with \l{QVariant}{QVariant()}, sets checkboxes with Qt::Checked or Qt::Unchecked
- \o {1, 3} \l{Qt::ItemDataRole}{enum Qt::ItemDataRole}
-
+ \o {1, 3} Qt::CheckStateRole
+ \o {1, 3} suppresses checkboxes with \l{QVariant}{QVariant()},
+ sets checkboxes with Qt::Checked or Qt::Unchecked
+ \o {1, 3} \l{Qt::ItemDataRole}{enum Qt::ItemDataRole}
\endtable
-Refer to Qt documentation to learn more about enum Qt::ItemDataRole's capabilities.
+ Refer to the Qt namespace documentation to learn more about the
+ Qt::ItemDataRole enum's capabilities.
+ Now we need to determine how using a seperated model impacts the
+ application's performance, so let's trace how often the view calls the
+ \l{QAbstractItemModel::}{data()} method. In order to track how often
+ the view calls the model, we have put a debug statement in the
+ \l{QAbstractItemModel::}{data()} method, which logs onto stdio. In
+ our small example, \l{QAbstractItemModel::}{data()} will be called 42
+ times.
+ Each time you hover the cursor over the field,
+ \l{QAbstractItemModel::}{data()} will be called again \mdash 7 times for
+ each cell. That's why it is important to make sure that your data is
+ available when \l{QAbstractItemModel::}{data()} is invoked and expensive
+ lookup operations are cached.
-Now we need to determine how using a seperated model impacts the application's performance, so let's trace how often the view calls the \l{QAbstractItemModel::data()}{data()} method. In order to track how often the view calls the model, we have put a debug statement in the \l{QAbstractItemModel::data()}{data()} method, which logs onto stdio. In our small example, \l{QAbstractItemModel::data()}{data()} will be called 42 times. Each time you hover the cursor over the field, \l{QAbstractItemModel::data()}{data()} will be called again - 7 times for each cell. That's why it is important to make sure that your data is available when \l{QAbstractItemModel::data()}{data()} is invoked and expensive lookup operations are cached.
+ \section2 2.3 A Clock inside a Table Cell
-\section2 2.3 A clock inside a table cell
-\image clock.png
+ \image clock.png
-We still have a read only table, but this time the content changes every second because we are showing the current time.
+ We still have a read only table, but this time the content changes every
+ second because we are showing the current time.
\snippet examples/tutorials/modelview/3_changingmodel/mymodel.cpp quoting mymodel_QVariant
-Something is missing to make the clock tick. We need to tell the view every second that the time has changed and that it needs to be read again. We do this with a timer. In the constructor, we set its interval to 1 second and it connect its timeout signal.
+ Something is missing to make the clock tick. We need to tell the view every
+ second that the time has changed and that it needs to be read again. We do
+ this with a timer. In the constructor, we set its interval to 1 second and
+ connect its timeout signal.
\snippet examples/tutorials/modelview/3_changingmodel/mymodel.cpp quoting mymodel_a
-Here is the corresponding slot:
+ Here is the corresponding slot:
\snippet examples/tutorials/modelview/3_changingmodel/mymodel.cpp quoting mymodel_b
-We ask the view to read the data in the top left cell again by emitting the \l{QAbstractItemModel::dataChanged()}{dataChanged()} signal. Note that we did not explicitly connect the \l{QAbstractItemModel::dataChanged()}{dataChanged()} signal to the view. This happened automatically when we called \l{QTableView::setModel()}{setModel()}.
+ We ask the view to read the data in the top left cell again by emitting the
+ \l{QAbstractItemModel::}{dataChanged()} signal. Note that we did not
+ explicitly connect the \l{QAbstractItemModel::}{dataChanged()} signal to
+ the view. This happened automatically when we called
+ \l{QTableView::}{setModel()}.
-\section2 2.4 Setting up Headers for Columns and Rows
-Headers can be hidden via a view method.
-\c tableView->verticalHeader()->hide();
-\image header.png
+ \section2 2.4 Setting up Headers for Columns and Rows
+ Headers can be hidden via a view method: \c{tableView->verticalHeader()->hide();}
+ \image header.png
-The header content, however, is set via the model, so we reimplement the \l{QAbstractItemModel::headerData()}{headerData()} method:
+ The header content, however, is set via the model, so we reimplement the
+ \l{QAbstractItemModel::headerData()}{headerData()} method:
\snippet examples/tutorials/modelview/4_headers/mymodel.cpp quoting mymodel_c
-\section2 2.5 The minimal Editing example
-In this example, we are going to build an application that automatically populates a window title with content by repeating values entered into table cells.
+ \section2 2.5 The Minimal Editing Example
+
+ In this example, we are going to build an application that automatically
+ populates a window title with content by repeating values entered into
+ table cells.
+
+ The model decides whether editing capabilities are available . We only have
+ to modify the model in order for the available editing capabilities to be
+ enabled. This is done by reimplementing the following virtual methods:
+ \l{QAbstractItemModel::}{setData()} and \l{QAbstractItemModel::}{flags()}.
-The model decides whether editing capabilities are available . We only have to modify the model in order for the available editing capabilities to be enabled. This is done by reimplementing the following virtual methods: \l{QAbstractItemModel::setData()}{setData()} and \l{QAbstractItemModel::flags()}{flags()}.
--------------------------------------------------------------mymodel.h---------------------
\snippet examples/tutorials/modelview/5_edit/mymodel.h Quoting ModelView Tutorial
-We use \c QStringList m_gridData to store our data. This makes \c m_gridData the core of MyModel. The rest of \c MyModel acts like a wrapper and adapts \c m_gridData to the QAbstractItemModel interface. We have also introduced the \l{QAbstractItemModel::editCompleted()}{editCompleted()} signal, which makes it possible to transfer the modified text to the window title.
+ We use \c QStringList m_gridData to store our data. This makes
+ \c m_gridData the core of MyModel. The rest of \c MyModel acts like a
+ wrapper and adapts \c m_gridData to the QAbstractItemModel interface. We
+ have also introduced the \c editCompleted() signal,
+ which makes it possible to transfer the modified text to the window title.
\snippet examples/tutorials/modelview/5_edit/mymodel.cpp quoting mymodel_d
-In the constructor, we fill \c QStringList gridData with 6 items. (one item for every field in the table)
+ In the constructor, we fill \c QStringList gridData with 6 items (one item
+ for every field in the table):
+
\snippet examples/tutorials/modelview/5_edit/mymodel.cpp quoting mymodel_e
-\l{QAbstractItemModel::setData()}{setData()} will be called each time the user edits a cell. The \c index parameter tells us which field has been edited and \c value provides the result of the editing process. The role will always be set to \c Qt::EditRole because our cells only contain text. If a checkbox were present and user permissions are set to allow the checkbox to be selected, calls would also be made with the role set to \c Qt::CheckStateRole.
- \snippet examples/tutorials/modelview/5_edit/mymodel.cpp quoting mymodel_f
+ \l{QAbstractItemModel::setData()}{setData()} will be called each time the
+ user edits a cell. The \c index parameter tells us which field has been
+ edited and \c value provides the result of the editing process. The role
+ will always be set to \c Qt::EditRole because our cells only contain text.
+ If a checkbox were present and user permissions are set to allow the
+ checkbox to be selected, calls would also be made with the role set to
+ \c Qt::CheckStateRole.
-Various properties of a cell can be adjusted with \l{QAbstractItemModel::flags()}{flags()}. Returning \c Qt::ItemIsEditable | \c Qt::ItemIsEnabled is enough to show an editor that a cell has been selected. If editing one cell modifies more data than the data in that particular cell, the model must emit a \l{QAbstractItemModel::dataChanged()}{dataChanged()} signal in order for the data that has been changed to be read.
+ \snippet examples/tutorials/modelview/5_edit/mymodel.cpp quoting mymodel_f
+ Various properties of a cell can be adjusted with
+ \l{QAbstractItemModel::flags()}{flags()}. Returning
+ \c Qt::ItemIsEditable | \c Qt::ItemIsEnabled is enough to show an editor
+ that a cell has been selected. If editing one cell modifies more data than
+ the data in that particular cell, the model must emit a
+ \l{QAbstractItemModel::}{dataChanged()} signal in order for the data that
+ has been changed to be read.
*/
/*!
\page modelview-part3.html
- \title ModelView Chapter 3 - Intermediate Topics
- \raw HTML
- <br>
- \endraw
-\section1 3 Intermediate Topics
- \raw HTML
- <br>
- \endraw
-\section2 3.1 TreeView
+ \contentspage {modelview-index.html}{Model/View Contents}
+ \previouspage {modelview-part2.html}{Developing a Simple Model/View Application}
+ \nextpage {modelview-part4.html}{Good Sources of Additional Information}
+ \title Model/View Chapter 3 - Intermediate Topics
+
+ \section1 3. Intermediate Topics
+
+ \section2 3.1 TreeView
-You can convert the example above into an application with a tree view. Simply replace QTableView with QTreeView, which results in a read/write tree. No changes have to be made to the model. The tree won't have any hierarchies because there aren't any hierarchies in the model itself.
-\image dummy_tree.png
+ You can convert the example above into an application with a tree view.
+ Simply replace QTableView with QTreeView, which results in a read/write
+ tree. No changes have to be made to the model. The tree won't have any
+ hierarchies because there aren't any hierarchies in the model itself.
+ \image dummy_tree.png
-QListView, QTableView and QTreeView all use a model abstraction, which is a merged list, table and tree. This makes it possible to use several different types of view classes from the same model.
-\image list_table_tree.png
+ QListView, QTableView and QTreeView all use a model abstraction, which is a
+ merged list, table and tree. This makes it possible to use several different
+ types of view classes from the same model.
+ \image list_table_tree.png
-This is how our example model looks so far:
-\image example_model.png
+ This is how our example model looks so far:
+ \image example_model.png
-We want to present a real tree. We have wrapped our data in the examples above in order to make a model. This time we use QStandardItemModel, which is a container for hierarchical data that also implements QAbstractItemModel. To show a tree, QStandardItemModel must be populated with \l{QStandardItem}{QStandardItems}, which are able to hold all the standard properties of items like text, fonts, checkboxes or brushes. \image tree_2_with_algorithm.png
--------------------------------------------------------------modelview.cpp---------------------
+ We want to present a real tree. We have wrapped our data in the examples
+ above in order to make a model. This time we use QStandardItemModel, which
+ is a container for hierarchical data that also implements
+ QAbstractItemModel. To show a tree, QStandardItemModel must be populated
+ with \l{QStandardItem}{QStandardItems}, which are able to hold all the
+ standard properties of items like text, fonts, checkboxes or brushes.
+ \image tree_2_with_algorithm.png
+
\snippet examples/tutorials/modelview/6_treeview/modelview.cpp Quoting ModelView Tutorial
-We simply instantiate a QStandardItemModel and add a couple of \l{QStandardItem}{QStandardItems} to the constructor. We can then make a hierarchical data structure because a QStandardItem can hold other \l{QStandardItem}{QStandardItems}. Nodes are collapsed and expanded within the view.
+ We simply instantiate a QStandardItemModel and add a couple of
+ \l{QStandardItem}{QStandardItems} to the constructor. We can then make a
+ hierarchical data structure because a QStandardItem can hold other
+ \l{QStandardItem}{QStandardItems}. Nodes are collapsed and expanded within
+ the view.
-\section2 3.2 Working with selection
+ \section2 3.2 Working with Selections
+ We want to access a selected item's content in order to output it into the
+ window title together with the hierarchy level.
+ \image selection2.png
-We want to access a selected item's content in order to output it into the window title together with the hierarchy level.
-\image selection2.png
+ So let's create a couple of items:
-So let's create a couple of items:
\snippet examples/tutorials/modelview/7_selections/modelview.cpp quoting modelview_a
-Views manage selections within a separate selection model, which can be retrieved with the \l{QAbstractItemView::selectionModel()}{selectionModel()} method. We retrieve the selection Model in order to connect a slot to its \l{QAbstractItemView::selectionChanged()}{selectionChanged()} signal.
- \snippet examples/tutorials/modelview/7_selections/modelview.cpp quoting modelview_b
+ Views manage selections within a separate selection model, which can be
+ retrieved with the \l{QAbstractItemView::}{selectionModel()}
+ method. We retrieve the selection Model in order to connect a slot to its
+ \l{QAbstractItemView::}{selectionChanged()} signal.
-We get the model index that corresponds to the selection by calling
-\l{QItemSelectionModel::currentIndex()}{treeView->selectionModel()->currentIndex()} and we get the the field's string by using the model index. Then we just calculate the item's \c hierarchyLevel. Top level items do not have parents and the \l{QAbstractItemModel::parent()}{parent()} method will return a default constructed \l{QModelIndex}{QModelIndex()}. This is why we use the \l{QAbstractItemModel::parent()}{parent()} method to iterate to the top level while counting the steps performed during iteration.
+ \snippet examples/tutorials/modelview/7_selections/modelview.cpp quoting modelview_b
-The selection model (as shown above) can be retrieved, but it can also be set with \l{QAbstractItemView}{QAbstractItemView::setSelectionModel}. This is how it's possible to have 3 view classes with synchronised selections because only one instance of a selection model is used. The instance of a selection model is retrieved from the first view class with \l{QAbstractItemView::selectionModel()}{selectionModel()} and the result is assigned to the second and third view class with \l{QAbstractItemView::setSelectionModel()}{setSelectionModel()};
-\section2 3.3 Predefined Models
-The typical way to use model/view is to wrap specific data to make it usable with view classes. Qt, however, also provides predefined models for common underlying data structures. If one of the available data structures is suitable for your application, a predefined model can be a good choice.
+ We get the model index that corresponds to the selection by calling
+ \l{QItemSelectionModel::currentIndex()}{treeView->selectionModel()->currentIndex()}
+ and we get the the field's string by using the model index. Then we just
+ calculate the item's \c hierarchyLevel. Top level items do not have
+ parents and the \l{QAbstractItemModel::}{parent()} method will return a
+ default constructed \l{QModelIndex}{QModelIndex()}. This is why we use the
+ \l{QAbstractItemModel::}{parent()} method to iterate to the top level while
+ counting the steps performed during iteration.
+
+ The selection model (as shown above) can be retrieved, but it can also be
+ set with \l{QAbstractItemView}{QAbstractItemView::setSelectionModel}. This
+ is how it's possible to have 3 view classes with synchronised selections
+ because only one instance of a selection model is used. The instance of a
+ selection model is retrieved from the first view class with
+ \l{QAbstractItemView::}{selectionModel()} and the result is assigned to the
+ second and third view class with \l{QAbstractItemView::}{setSelectionModel()}.
+
+ \section2 3.3 Predefined Models
+
+ The typical way to use model/view is to wrap specific data to make it
+ usable with view classes. Qt, however, also provides predefined models for
+ common underlying data structures. If one of the available data structures
+ is suitable for your application, a predefined model can be a good choice.
\table
\row
@@ -331,11 +507,8 @@ The typical way to use model/view is to wrap specific data to make it usable wit
\o QStandardItemModel
\o Stores arbitrary hierarchical items
\row
- \o QFileSystemModel
- \raw HTML
- <br>
- \endraw
- QDirModel
+ \o QFileSystemModel\br
+ QDirModel
\o Encapsulate the local file system
\row
\o QSqlQueryModel
@@ -352,108 +525,144 @@ The typical way to use model/view is to wrap specific data to make it usable wit
\endtable
-\section2 3.4 Delegates
-In all examples so far, data is presented as text or a checkbox in a cell and is edited as text or a checkbox. The component that provides these presentation and editing services is called a “delegate.” We are only just beginning to work with the delegate because the view uses a default delegate. But imagine that we want to have a different editor.(e.g. a slider or a drop down list) Or imagine that we want to present data as graphics. Let's take a look at an example called Stardelegate, ( \l{http://qt.nokia.com/doc/4.6/itemviews-stardelegate.html}{http://qt.nokia.com/doc/4.6/itemviews-stardelegate.html} ) in which stars are used to show a rating: \image stardelegate.png
-
-The view has a method that replaces the default delegate and installs a custom delegate. This method is called \l{QAbstractItemView::setItemDelegate()}{setItemDelegate()}. A new delegate can be written by creating a class that inherits from QStyledItemDelegate. In order to write a delegate that displays stars and has no input capabilities, we only need to overwrite 2 methods.
-
-\code
- class StarDelegate : public QStyledItemDelegate
- {
- Q_OBJECT
- public:
- StarDelegate(QWidget *parent = 0);
- void paint(QPainter *painter, const QStyleOptionViewItem &option,
- const QModelIndex &index) const;
- QSize sizeHint(const QStyleOptionViewItem &option,
+ \section2 3.4 Delegates
+
+ In all examples so far, data is presented as text or a checkbox in a cell
+ and is edited as text or a checkbox. The component that provides these
+ presentation and editing services is called a \e delegate. We are only just
+ beginning to work with the delegate because the view uses a default
+ delegate. But imagine that we want to have a different editor.(e.g. a
+ slider or a drop down list) Or imagine that we want to present data as
+ graphics. Let's take a look at an example called
+ \l{Star Delegate Example}{Star Delegate}, in which stars are used to show
+ a rating:
+ \image stardelegate.png
+
+ The view has a method that replaces the default delegate and installs a
+ custom delegate. This method is called
+ \l{QAbstractItemView::}{setItemDelegate()}. A new delegate can be written
+ by creating a class that inherits from QStyledItemDelegate. In order to
+ write a delegate that displays stars and has no input capabilities, we only
+ need to overwrite 2 methods.
+
+ \code
+ class StarDelegate : public QStyledItemDelegate
+ {
+ Q_OBJECT
+ public:
+ StarDelegate(QWidget *parent = 0);
+ void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
- };
-
-\endcode
-
-\l{QStyledItemDelegate::paint()}{paint()} draws stars depending on the content of the underlying data. The data can be looked up with parameter \l{QModelIndex::data()}{index.data()}. \l{QAbstractItemDelegate::sizeHint()}{sizeHint} specifies the stars dimensions so the the cell will provide enough height and width to accommodate the stars.
+ QSize sizeHint(const QStyleOptionViewItem &option,
+ const QModelIndex &index) const;
+ };
+ \endcode
-Writing custom delegates is the right choice if you want to show your data with a custom graphical representation inside the grid of the view class. If you want to leave the grid, you can write a custom view class.
+ \l{QStyledItemDelegate::}{paint()} draws stars depending on the content
+ of the underlying data. The data can be looked up with parameter
+ \l{QModelIndex::data()}{index.data()}.
+ \l{QAbstractItemDelegate::}{sizeHint()} specifies each star's dimensions
+ so the the cell will provide enough height and width to accommodate the
+ stars.
-\section2 3.5 Debugging with ModelTest
-The passive nature of models provides new challenges for programmers. Inconsistencies in the model can cause the application to crash. Since the model is hit by numerous calls from the view, it is hard to find out which call has crashed the application and which operation has introduced the problem.
+ Writing custom delegates is the right choice if you want to show your data
+ with a custom graphical representation inside the grid of the view class.
+ If you want to leave the grid, you can write a custom view class.
-Qt provides software called ModelTest, which checks models while your programming is running. Every time the model is changed, ModelTest scans the model and reports errors with an assert. This is especially important for tree models, since their hierarchical nature leaves many possibilities for subtle inconsistencies. \l http://labs.qt.nokia.com/page/Projects/Itemview/Modeltest
+ \section2 3.5 Debugging with ModelTest
-Unlike view classes, ModelTest uses out of range indexes to test the model. This means your application may crash with ModelTest even if it runs perfectly without it. So you also need to handle all of the indexes that are out of range when using ModelTest.
+ The passive nature of models provides new challenges for programmers.
+ Inconsistencies in the model can cause the application to crash. Since the
+ model is hit by numerous calls from the view, it is hard to find out which
+ call has crashed the application and which operation has introduced the
+ problem.
+ Qt provides software called
+ \l{http://labs.qt.nokia.com/page/Projects/Itemview/Modeltest}{ModelTest},
+ which checks models while your programming is running. Every time the model
+ is changed, ModelTest scans the model and reports errors with an assert.
+ This is especially important for tree models, since their hierarchical
+ nature leaves many possibilities for subtle inconsistencies.
-
-\raw HTML
-<br>
-\endraw
+ Unlike view classes, ModelTest uses out of range indexes to test the model.
+ This means your application may crash with ModelTest even if it runs
+ perfectly without it. So you also need to handle all of the indexes that
+ are out of range when using ModelTest.
+ \section2 3.6 Model/View NG
+ \raw HTML
+ <table style="background-color:white;border:none;font: normal 13px/1.2 Verdana;">
+ <tr><td align="left" valign="top" style="background-color:white;border:none;padding:5px;">
+ \endraw
+ \raw HTML
+ <!-- wrap content table p has 0 padding and the padding for p outside of the table is 5px-->
+ \endraw
-\section2 3.6 Model/View NG
+ Model/View was introduced in Qt 4.0 and is a frequently used technology.
+ Feedback from developers and new development trends have shown that there
+ is a need to further develop the model/view technology. Therefore a
+ research project originated at Nokia is looking into ways to go beyond the
+ current implementation.
+
+ One limitation of model/view is that view classes are basically all fixed
+ grids. It is possible, but really hard to make a list view with icons
+ placed on a curve; or cells expanding on mouse over events to show
+ additional information.
+ In order to achieve graphically rich view experiences, Model/View NG will
+ use QGraphicsView to render elements. Nodel/View NG also aims to make
+ model/view programming more intuitive. One way to achieve this is to have
+ separate models for lists, tables and trees. The current model abstraction
+ is complex because it is capable of representing a list, a table or a tree.
+
+ Model/View NG is a research project. You are welcome to checkout the source
+ code, monitor progress and take part in discussions at the following
+ address: \l{http://labs.qt.nokia.com/page/Projects/Itemview/ItemviewsNG}
-\raw HTML
-<table style="background-color:white;border:none;font: normal 13px/1.2 Verdana;">
-<tr><td align="left" valign="top" style="background-color:white;border:none;padding:5px;">
-\endraw
+ \raw HTML
+ </td><td align="right" valign="top">
+ \endraw
-\raw HTML
-<!-- wrap content table p has 0 padding and the padding for p outside of the table is 5px-->
-\endraw
+ \inlineimage path.png
-Model/View was introduced in Qt 4.0 and is a frequently used technology. Feedback from clients and new development trends have shown, that there is a need to further develop the model/view technology. Therefore a research project at Nokia is looking into ways to go beyond the current implementation.
-\raw HTML
-<br>
-\endraw
-One limitation of model/view is that view classes are basically all fixed grids. It is possible, but really hard to make a list view with icons placed on a curve; or cells expanding on mouse over events to show additional information. In order to achieve graphically rich view experiences, Model/View NG will use QGraphicsView to render elements. Nodel/View NG also aims to make model/view programming more intuitive. One way to achieve this is to have separate models for lists, tables and trees. The current model abstraction is complex because it is capable of representing a list, a table or a tree.
-\raw HTML
-<br>
-\endraw
-Model/View NG is a research project. You are welcome to checkout the source code, monitor progress and take part in discussions at the following address: \l{http://labs.qt.nokia.com/page/Projects/Itemview/ItemviewsNG}{http://labs.qt.nokia.com/page/Projects/Itemview/ItemviewsNG}
+ \raw HTML
+ </td></tr></table>
+ \endraw
+*/
-\raw HTML
-</td><td align="right" valign="top">
-\endraw
+/*!
+ \page modelview-part4.html
+ \contentspage {modelview-index.html}{Model/View Contents}
+ \previouspage {modelview-part3.html}{Intermediate Topics}
+ \title Model/View Chapter 4 - Good Sources of Additional Information
-\inlineimage path.png
+ \section1 4. Good Sources of Additional Information
-\raw HTML
-</td></tr></table>
-\endraw
-\raw HTML
-<br>
-\endraw
+ \section2 4.1 Books
-*/
+ Model/View programming is covered quite extensively in the documentation of
+ Qt but also in several good books.
-/*!
- \page modelview-part4.html
-
- \title ModelView Chapter 4 - Good Sources for Additional Information
-
- \raw HTML
- <br>
- \endraw
-\section1 4 Good Sources for Additional Information
-\raw HTML
-<br>
-\endraw
-\section2 4.1 Books
-Model/View programming is covered quite extensively in the documentation of Qt but also in several good books.
\list 1
- \o C++ GUI Programming with Qt 4 / Jasmin Blanchette, Mark Summerfield, Prentice Hall, 2nd edition, ISBN 0-13-235416-0
-also available in German: C++ GUI Programmierung mit Qt 4: Die offizielle Einführung, Addison-Wesley, ISBN 3-827327-29-6
- \o The Book of Qt4, The Art of Building Qt Applications / Daniel Molkentin, Open Source Press ISBN 1-59327-147-6
-Translated from: Qt 4, Einführung in die Applikationsentwicklung, Open Source Press, ISBN 3-937514-12-0
- \o Foundations of Qt Development / Johan Thelin, Apress, ISBN 1-59059-831-8
+ \o \bold{C++ GUI Programming with Qt 4} / Jasmin Blanchette, Mark Summerfield,
+ \e{Prentice Hall, 2nd edition}, ISBN 0-13-235416-0. Also available in
+ German: C++ GUI Programmierung mit Qt 4: Die offizielle Einführung,
+ \e{Addison-Wesley}, ISBN 3-827327-29-6
+ \o \bold{The Book of Qt4, The Art of Building Qt Applications} / Daniel Molkentin,
+ \e{Open Source Press}, ISBN 1-59327-147-6.
+ Translated from \bold{Qt 4, Einführung in die Applikationsentwicklung},
+ \e{Open Source Press}, ISBN 3-937514-12-0.
+ \o \bold{Foundations of Qt Development} / Johan Thelin, \e{Apress}, ISBN 1-59059-831-8.
\endlist
-\raw HTML
-<br>
-\endraw
-The following list provides an overview of example programs contained in the books above. Some of them make very good templates for developing similar applications.
+ More information about these books is available on the
+ \l{Books about Qt Programming}{Qt Web site}.
+
+ The following list provides an overview of example programs contained in the
+ books above. Some of them make very good templates for developing similar
+ applications.
\table
\header
@@ -461,45 +670,45 @@ The following list provides an overview of example programs contained in the boo
\o view class used
\o model used
\o aspects touched
- \o
+ \o
\row
\o Team Leaders
\o QListview
\o QStringListModel
- \o
+ \o
\o Book 1, Chapter 10, Figure 10.6
\row
\o Directory Viewer
\o QTreeView
\o QDirModel
- \o
+ \o
\o Book 1, Chapter 10, Figure 10.7
\row
\o Color Names
\o QListView
\o QSortFilterProxyModel
-applied to QStringListModel
- \o
+ applied to QStringListModel
+ \o
\o Book 1, Chapter 10, Figure 10.8
\row
\o Currencies
\o QTableView
\o custom model based on
-QAbstractTableModel
+ QAbstractTableModel
\o read only
\o Book 1, Chapter 10, Figure 10.10
\row
\o Cities
\o QTableView
\o custom model based on
-QAbstractTableModel
+ QAbstractTableModel
\o read / write
\o Book 1, Chapter 10, Figure 10.12
\row
\o Boolean Parser
\o QTreeView
\o custom model based on
-QAbstractItemModel
+ QAbstractItemModel
\o read only
\o Book 1, Chapter 10, Figure 10.14
\row
@@ -511,141 +720,129 @@ QAbstractItemModel
\row
\o Four directory views
\o QListView
-QTableView
-QTreeView
+ QTableView
+ QTreeView
\o QDirModel
\o demonstrates the use of multiple views
\o Book2, Chapter 8.2
\row
\o Address Book
\o QListView
-QTableView
-QTreeView
+ QTableView
+ QTreeView
\o custom model based on
-QAbstractTableModel
+ QAbstractTableModel
\o read / write
\o Book2, Chapter 8.4
\row
\o Address Book with sorting
- \o
+ \o
\o QProxyModel
\o introducing sort and filter capabilities
\o Book2, Chapter 8.5
\row
- \o Address Book
-with checkboxes
- \o
- \o
- \o introducing checkboxes
-in model/view
+ \o Address Book
+ with checkboxes
+ \o
+ \o
+ \o introducing checkboxes in model/view
\o Book2, Chapter 8.6
\row
- \o Address Book
-with transposed grid
- \o
+ \o Address Book with transposed grid
+ \o
\o custom proxy Model based on QAbstractProxyModel
\o introducing a custom model
\o Book2, Chapter 8.7
\row
- \o Address Book
-with drag and drop
- \o
- \o
+ \o Address Book with drag and drop
+ \o
+ \o
\o introducing drag and drop support
\o Book2, Chapter 8.8
\row
\o Address Book with custom editor
- \o
- \o
+ \o
+ \o
\o introducing custom delegates
\o Book2, Chapter 8.9
\row
\o Views
\o QListView
-QTableView
-QTreeView
+ QTableView
+ QTreeView
\o QStandardItemModel
\o read only
\o Book 3, Chapter 5, figure 5-3
\row
\o Bardelegate
\o QTableView
- \o
+ \o
\o custom delegate for presentation based on QAbstractItemDelegate
\o Book 3, Chapter 5, figure 5-5
\row
\o Editdelegate
\o QTableView
- \o
+ \o
\o custom delegate for editing based on QAbstractItemDelegate
\o Book 3, Chapter 5, figure 5-6
\row
\o Singleitemview
- \o custom view based on
-QAbstractItemView
- \o
+ \o custom view based on QAbstractItemView
+ \o
\o custom view
\o Book 3,
-Chapter 5,
-figure 5-7
+ Chapter 5,
+ figure 5-7
\row
\o listmodel
\o QTableView
- \o custom Model based on
-QAbstractTableModel
+ \o custom Model based on QAbstractTableModel
\o read only
- \o Book 3,
-Chapter 5,
-Figure 5-8
+ \o Book 3, Chapter 5, Figure 5-8
\row
\o treemodel
\o QTreeView
- \o custom Model based on
-QAbstractItemModel
+ \o custom Model based on QAbstractItemModel
\o read only
- \o Book 3,
-Chapter 5,
-Figure 5-10
+ \o Book 3, Chapter 5, Figure 5-10
\row
\o edit integers
\o QListView
- \o custom Model based on
-QAbstractListModel
+ \o custom Model based on QAbstractListModel
\o read / write
- \o Book 3,
-Chapter 5,
-Listing 5-37, Figure 5-11
+ \o Book 3, Chapter 5, Listing 5-37, Figure 5-11
\row
\o sorting
\o QTableView
- \o QSortFilterProxyModel
-applied to QStringListModel
+ \o QSortFilterProxyModel applied to QStringListModel
\o demonstrates sorting
\o Book 3, Chapter 5, Figure 5-12
-
\endtable
-\section2 4.2 Qt documentation
-Qt 4.6 comes with 17 examples and 2 Demonstrations for model/view. The examples can be found here: \l{http://doc.qt.nokia.com/4.6/examples-itemviews.html}{http://doc.qt.nokia.com/4.6/examples-itemviews.html}
+ \section2 4.2 Qt Documentation
+
+ Qt 4.7 comes with 17 examples and 2 Demonstrations for model/view.
+ The examples can be found here: \l{Item Views Examples}
\table
\header
- \o example name
- \o view class used
- \o model used
- \o aspects touched
+ \o Example name
+ \o View class used
+ \o Model used
+ \o Aspects touched
\row
\o Address Book
\o QTableView
\o QAbstractTableModel
-QSortFilterProxyModel
- \o usage of QSortFilterProxyModel to generate different subsets from one data pool
+ QSortFilterProxyModel
+ \o usage of QSortFilterProxyModel to generate different
+ subsets from one data pool
\row
\o Basic Sort/Filter Model
\o QTreeView
\o QStandardItemModel
-QSortFilterProxyModel
- \o
+ QSortFilterProxyModel
+ \o
\row
\o Chart
\o custom view
@@ -664,9 +861,8 @@ QSortFilterProxyModel
\o Custom Sort/Filter Model
\o QTreeView
\o QStandardItemModel
-QSortFilterProxyModel
- \o subclass QSortFilterProxyModel
-for advanced sorting and filtering
+ QSortFilterProxyModel
+ \o subclass QSortFilterProxyModel for advanced sorting and filtering
\row
\o Dir View
\o QTreeView
@@ -676,17 +872,19 @@ for advanced sorting and filtering
\o Editable Tree Model
\o QTreeView
\o custom tree model
- \o comprehensive example for working with trees, demonstrates editing cells and tree structure with an underlying custom model
+ \o comprehensive example for working with trees, demonstrates
+ editing cells and tree structure with an underlying custom
+ model
\row
\o Fetch More
\o QListView
\o custom list model
- \o dynamically changing model
+ \o dynamically changing model
\row
\o Frozen Column
\o QTableView
\o QStandardItemModel
- \o
+ \o
\row
\o Pixelator
\o QTableView
@@ -723,13 +921,18 @@ for advanced sorting and filtering
\o comprehensive custom delegate example.
\endtable
-Demonstrations are similar to examples except that no walk-through is provided for the code lines. Demonstrations are also sometimes more feature rich.
- \l{http://doc.qt.nokia.com/4.6/demos.html}{http://doc.qt.nokia.com/4.6/demos.html}
+ \l{Qt Demonstrations}{Demonstrations} are similar to examples except
+ that no walkthrough is provided for the code. Demonstrations are also
+ sometimes more feature rich.
+
\list
- \o The \bold Interview demonstration shows the same model and selection being shared between three different views.
- \o Demonstration \bold Spreadsheet demonstrates the use of a table view as a spreadsheet, using custom delegates to render each item according to the type of data it contains.
+ \o The \bold Interview demonstration shows the same model and
+ selection being shared between three different views.
+ \o Demonstration \bold Spreadsheet demonstrates the use of a
+ table view as a spreadsheet, using custom delegates to render
+ each item according to the type of data it contains.
\endlist
-A reference documentation for model/view technology is also available. \l{http://doc.qt.nokia.com/4.6/model-view-programming.html}{http://doc.qt.nokia.com/4.6/model-view-programming.html}
-
-*/ \ No newline at end of file
+ A \l{Model/View Programming}{reference document} for model/view technology
+ is also available.
+*/
diff --git a/examples/tutorials/modelview/1_readonly/main.cpp b/examples/tutorials/modelview/1_readonly/main.cpp
index ad11f38..fb4726a 100755
--- a/examples/tutorials/modelview/1_readonly/main.cpp
+++ b/examples/tutorials/modelview/1_readonly/main.cpp
@@ -39,6 +39,7 @@
****************************************************************************/
//! [Quoting ModelView Tutorial]
+// main.cpp
#include <QtGui/QApplication>
#include "modelview.h"
diff --git a/examples/tutorials/modelview/1_readonly/modelview.cpp b/examples/tutorials/modelview/1_readonly/modelview.cpp
index 027be56..91a97bf 100755
--- a/examples/tutorials/modelview/1_readonly/modelview.cpp
+++ b/examples/tutorials/modelview/1_readonly/modelview.cpp
@@ -39,6 +39,7 @@
****************************************************************************/
//! [Quoting ModelView Tutorial]
+// modelview.cpp
#include <QTableView>
#include "modelview.h"
#include "mymodel.h"
@@ -48,6 +49,6 @@ ModelView::ModelView(QWidget *parent)
{
tableView = new QTableView(this);
setCentralWidget(tableView);
- tableView->setModel(new MyModel(this) );
+ tableView->setModel(new MyModel(this));
}
-//! [Quoting ModelView Tutorial] \ No newline at end of file
+//! [Quoting ModelView Tutorial]
diff --git a/examples/tutorials/modelview/1_readonly/modelview.h b/examples/tutorials/modelview/1_readonly/modelview.h
index d0f96cd..9307083 100755
--- a/examples/tutorials/modelview/1_readonly/modelview.h
+++ b/examples/tutorials/modelview/1_readonly/modelview.h
@@ -38,11 +38,11 @@
**
****************************************************************************/
-//! [Quoting ModelView Tutorial]
#ifndef MODELVIEW_H
#define MODELVIEW_H
-
+//! [Quoting ModelView Tutorial]
+// modelview.h
#include <QtGui/QMainWindow>
class QTableView; //forward declaration
@@ -56,6 +56,6 @@ public:
ModelView(QWidget *parent = 0);
};
+//! [Quoting ModelView Tutorial]
#endif // MODELVIEW_H
-//! [Quoting ModelView Tutorial] \ No newline at end of file
diff --git a/examples/tutorials/modelview/1_readonly/mymodel.cpp b/examples/tutorials/modelview/1_readonly/mymodel.cpp
index c441720..394605a 100755
--- a/examples/tutorials/modelview/1_readonly/mymodel.cpp
+++ b/examples/tutorials/modelview/1_readonly/mymodel.cpp
@@ -39,6 +39,7 @@
****************************************************************************/
//! [Quoting ModelView Tutorial]
+// mymodel.cpp
#include "mymodel.h"
MyModel::MyModel(QObject *parent)
@@ -46,22 +47,19 @@ MyModel::MyModel(QObject *parent)
{
}
-//-------------------------------------------------------
-int MyModel::rowCount(const QModelIndex & /*parent*/ ) const
+int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
return 2;
}
-//-------------------------------------------------------
-int MyModel::columnCount(const QModelIndex & /*parent*/ ) const
+int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
return 3;
}
-//-------------------------------------------------------
-QVariant MyModel::data(const QModelIndex &index, int role ) const
+QVariant MyModel::data(const QModelIndex &index, int role) const
{
- if(role == Qt::DisplayRole)
+ if (role == Qt::DisplayRole)
{
return QString("Row%1, Column%2")
.arg(index.row() + 1)
@@ -69,4 +67,4 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
}
return QVariant();
}
-//! [Quoting ModelView Tutorial] \ No newline at end of file
+//! [Quoting ModelView Tutorial]
diff --git a/examples/tutorials/modelview/1_readonly/mymodel.h b/examples/tutorials/modelview/1_readonly/mymodel.h
index c0ddf4ac6..6065f6e 100755
--- a/examples/tutorials/modelview/1_readonly/mymodel.h
+++ b/examples/tutorials/modelview/1_readonly/mymodel.h
@@ -38,10 +38,11 @@
**
****************************************************************************/
-//! [Quoting ModelView Tutorial]
#ifndef MYMODEL_H
#define MYMODEL_H
+//! [Quoting ModelView Tutorial]
+// mymodel.h
#include <QAbstractTableModel>
class MyModel : public QAbstractTableModel
@@ -53,6 +54,6 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
};
+//! [Quoting ModelView Tutorial]
#endif // MYMODEL_H
-//! [Quoting ModelView Tutorial] \ No newline at end of file
diff --git a/examples/tutorials/modelview/2_formatting/modelview.cpp b/examples/tutorials/modelview/2_formatting/modelview.cpp
index 2b05d4c..9a5ce64 100755
--- a/examples/tutorials/modelview/2_formatting/modelview.cpp
+++ b/examples/tutorials/modelview/2_formatting/modelview.cpp
@@ -47,6 +47,6 @@ ModelView::ModelView(QWidget *parent)
{
tableView = new QTableView(this);
setCentralWidget(tableView);
- tableView->setModel(new MyModel(this) );
+ tableView->setModel(new MyModel(this));
}
diff --git a/examples/tutorials/modelview/2_formatting/mymodel.cpp b/examples/tutorials/modelview/2_formatting/mymodel.cpp
index e9e68de..e34e014 100755
--- a/examples/tutorials/modelview/2_formatting/mymodel.cpp
+++ b/examples/tutorials/modelview/2_formatting/mymodel.cpp
@@ -44,25 +44,23 @@
#include <QDebug>
//! [Quoting ModelView Tutorial]
+// mymodel.cpp
MyModel::MyModel(QObject *parent)
:QAbstractTableModel(parent)
{
}
-//-------------------------------------------------------
-int MyModel::rowCount(const QModelIndex & /*parent */ ) const
+int MyModel::rowCount(const QModelIndex & /*parent */) const
{
return 2;
}
-//-------------------------------------------------------
-int MyModel::columnCount(const QModelIndex & /*parent */ ) const
+int MyModel::columnCount(const QModelIndex & /*parent */) const
{
return 3;
}
-//-------------------------------------------------------
-QVariant MyModel::data(const QModelIndex &index, int role ) const
+QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
@@ -72,15 +70,15 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
switch(role){
case Qt::DisplayRole:
- if(row == 0 && col == 1 )return QString("<--left");
- if(row == 1 && col == 1 )return QString("right-->");
+ if (row == 0 && col == 1) return QString("<--left");
+ if (row == 1 && col == 1) return QString("right-->");
return QString("Row%1, Column%2")
.arg(row + 1)
.arg(col +1);
break;
case Qt::FontRole:
- if(row == 0 && col ==0 ) //change font only for cell(0,0)
+ if (row == 0 && col == 0) //change font only for cell(0,0)
{
QFont boldFont;
boldFont.setBold(true);
@@ -89,7 +87,7 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
break;
case Qt::BackgroundRole:
- if(row == 1 && col ==2 ) //change background only for cell(1,2)
+ if (row == 1 && col == 2) //change background only for cell(1,2)
{
QBrush redBackground(QColor(Qt::red));
return redBackground;
@@ -97,14 +95,14 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
break;
case Qt::TextAlignmentRole:
- if(row == 1 && col ==1 ) //change text alignment only for cell(1,1)
+ if (row == 1 && col == 1) //change text alignment only for cell(1,1)
{
return Qt::AlignRight + Qt::AlignVCenter;
}
break;
case Qt::CheckStateRole:
- if(row == 1 && col ==0 ) //add a checkbox to cell(1,0)
+ if (row == 1 && col == 0) //add a checkbox to cell(1,0)
{
return Qt::Checked;
}
diff --git a/examples/tutorials/modelview/3_changingmodel/modelview.cpp b/examples/tutorials/modelview/3_changingmodel/modelview.cpp
index 2b05d4c..9a5ce64 100755
--- a/examples/tutorials/modelview/3_changingmodel/modelview.cpp
+++ b/examples/tutorials/modelview/3_changingmodel/modelview.cpp
@@ -47,6 +47,6 @@ ModelView::ModelView(QWidget *parent)
{
tableView = new QTableView(this);
setCentralWidget(tableView);
- tableView->setModel(new MyModel(this) );
+ tableView->setModel(new MyModel(this));
}
diff --git a/examples/tutorials/modelview/3_changingmodel/mymodel.cpp b/examples/tutorials/modelview/3_changingmodel/mymodel.cpp
index d806945..42915b0 100755
--- a/examples/tutorials/modelview/3_changingmodel/mymodel.cpp
+++ b/examples/tutorials/modelview/3_changingmodel/mymodel.cpp
@@ -50,32 +50,32 @@ MyModel::MyModel(QObject *parent)
// selectedCell = 0;
timer = new QTimer(this);
timer->setInterval(1000);
- connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()) );
+ connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()));
timer->start();
}
//! [quoting mymodel_a]
//-------------------------------------------------------
-int MyModel::rowCount(const QModelIndex & /*parent */ ) const
+int MyModel::rowCount(const QModelIndex & /*parent */) const
{
return 2;
}
//-------------------------------------------------------
-int MyModel::columnCount(const QModelIndex & /*parent */ ) const
+int MyModel::columnCount(const QModelIndex & /*parent */) const
{
return 3;
}
//-------------------------------------------------------
//! [quoting mymodel_QVariant ]
-QVariant MyModel::data(const QModelIndex &index, int role ) const
+QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
- if(role == Qt::DisplayRole)
+ if (role == Qt::DisplayRole)
{
- if(row == 0 && col == 0 )
+ if (row == 0 && col == 0)
{
return QTime::currentTime().toString();
}
@@ -88,8 +88,8 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
void MyModel::timerHit()
{
//we identify the top left cell
- QModelIndex topLeft = createIndex ( 0,0 );
+ QModelIndex topLeft = createIndex(0,0);
//emit a signal to make the view reread identified data
- emit dataChanged ( topLeft, topLeft );
+ emit dataChanged(topLeft, topLeft);
}
//! [quoting mymodel_b ]
diff --git a/examples/tutorials/modelview/4_headers/modelview.cpp b/examples/tutorials/modelview/4_headers/modelview.cpp
index f661ab5..449dbbc 100755
--- a/examples/tutorials/modelview/4_headers/modelview.cpp
+++ b/examples/tutorials/modelview/4_headers/modelview.cpp
@@ -48,7 +48,7 @@ ModelView::ModelView(QWidget *parent)
{
tableView = new QTableView(this);
setCentralWidget(tableView);
- tableView->setModel(new MyModel(this) );
+ tableView->setModel(new MyModel(this));
tableView->verticalHeader()->hide();
}
diff --git a/examples/tutorials/modelview/4_headers/mymodel.cpp b/examples/tutorials/modelview/4_headers/mymodel.cpp
index 94fde34..e6f977d 100755
--- a/examples/tutorials/modelview/4_headers/mymodel.cpp
+++ b/examples/tutorials/modelview/4_headers/mymodel.cpp
@@ -46,21 +46,21 @@ MyModel::MyModel(QObject *parent)
}
//-------------------------------------------------------
-int MyModel::rowCount(const QModelIndex & /*parent*/ ) const
+int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
return 2;
}
//-------------------------------------------------------
-int MyModel::columnCount(const QModelIndex & /*parent*/ ) const
+int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
return 3;
}
//-------------------------------------------------------
-QVariant MyModel::data(const QModelIndex &index, int role ) const
+QVariant MyModel::data(const QModelIndex &index, int role) const
{
- if(role == Qt::DisplayRole)
+ if (role == Qt::DisplayRole)
{
return QString("Row%1, Column%2")
.arg(index.row() + 1)
@@ -88,4 +88,4 @@ QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role)
}
return QVariant();
}
-//! [quoting mymodel_c] \ No newline at end of file
+//! [quoting mymodel_c]
diff --git a/examples/tutorials/modelview/5_edit/modelview.cpp b/examples/tutorials/modelview/5_edit/modelview.cpp
index d8853c9..a6c6ef5 100755
--- a/examples/tutorials/modelview/5_edit/modelview.cpp
+++ b/examples/tutorials/modelview/5_edit/modelview.cpp
@@ -48,13 +48,13 @@ ModelView::ModelView(QWidget *parent)
tableView = new QTableView(this);
setCentralWidget(tableView);
QAbstractTableModel *myModel = new MyModel(this);
- tableView->setModel( myModel );
+ tableView->setModel(myModel);
//transfer changes to the model to the window title
- connect(myModel, SIGNAL(editCompleted(const QString &) ), this, SLOT(setWindowTitle(const QString &)));
+ connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &)));
}
void ModelView::showWindowTitle(const QString & title)
{
-setWindowTitle( title );
+setWindowTitle(title);
}
diff --git a/examples/tutorials/modelview/5_edit/mymodel.cpp b/examples/tutorials/modelview/5_edit/mymodel.cpp
index 6007da1..67181ca 100755
--- a/examples/tutorials/modelview/5_edit/mymodel.cpp
+++ b/examples/tutorials/modelview/5_edit/mymodel.cpp
@@ -53,22 +53,19 @@ MyModel::MyModel(QObject *parent)
//! [quoting mymodel_d]
//! [quoting mymodel_e]
-//-------------------------------------------------------
-int MyModel::rowCount(const QModelIndex & /*parent*/ ) const
+int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
return ROWS;
}
-//-------------------------------------------------------
-int MyModel::columnCount(const QModelIndex & /*parent*/ ) const
+int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
return COLS;
}
-//-------------------------------------------------------
-QVariant MyModel::data(const QModelIndex &index, int role ) const
+QVariant MyModel::data(const QModelIndex &index, int role) const
{
- if(role == Qt::DisplayRole)
+ if (role == Qt::DisplayRole)
{
return m_gridData[modelIndexToOffset(index)];
}
@@ -79,23 +76,21 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const
//-----------------------------------------------------------------
//! [quoting mymodel_f]
-bool MyModel::setData ( const QModelIndex & index, const QVariant & value, int role )
+bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
- if(role == Qt::EditRole)
+ if (role == Qt::EditRole)
{
m_gridData[modelIndexToOffset(index)] = value.toString();
- emit editCompleted(m_gridData.join(" | ") );
+ emit editCompleted(m_gridData.join(" | "));
}
return true;
}
-//-----------------------------------------------------------------
-Qt::ItemFlags MyModel::flags ( const QModelIndex & /*index*/ ) const
+Qt::ItemFlags MyModel::flags(const QModelIndex & /*index*/) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
}
-//-----------------------------------------------------------------
//convert row and column information to array offset
int MyModel::modelIndexToOffset(const QModelIndex & index) const
{
diff --git a/examples/tutorials/modelview/5_edit/mymodel.h b/examples/tutorials/modelview/5_edit/mymodel.h
index 54f2b30..0d2a1b8 100755
--- a/examples/tutorials/modelview/5_edit/mymodel.h
+++ b/examples/tutorials/modelview/5_edit/mymodel.h
@@ -38,10 +38,11 @@
**
****************************************************************************/
-//! [Quoting ModelView Tutorial]
#ifndef MYMODEL_H
#define MYMODEL_H
+//! [Quoting ModelView Tutorial]
+// mymodel.h
#include <QAbstractTableModel>
#include <QStringList>
@@ -53,14 +54,14 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
- bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
- Qt::ItemFlags flags ( const QModelIndex & index ) const ;
+ bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
+ Qt::ItemFlags flags(const QModelIndex & index) const ;
private:
QStringList m_gridData; //holds text entered into QTableView
int modelIndexToOffset(const QModelIndex & index) const;
signals:
void editCompleted(const QString &);
};
+//! [Quoting ModelView Tutorial]
#endif // MYMODEL_H
-//! [Quoting ModelView Tutorial] \ No newline at end of file
diff --git a/examples/tutorials/modelview/6_treeview/modelview.cpp b/examples/tutorials/modelview/6_treeview/modelview.cpp
index a25e4e9..772dbdd 100755
--- a/examples/tutorials/modelview/6_treeview/modelview.cpp
+++ b/examples/tutorials/modelview/6_treeview/modelview.cpp
@@ -39,6 +39,7 @@
****************************************************************************/
//! [Quoting ModelView Tutorial]
+// modelview.cpp
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
@@ -64,14 +65,13 @@ ModelView::ModelView(QWidget *parent)
// adding a row to an item starts a subtree
preparedColumn.first()->appendRow(secondRow);
- treeView->setModel( standardModel );
+ treeView->setModel(standardModel);
treeView->expandAll();
}
-//---------------------------------------------------------------------------
QList<QStandardItem *> ModelView::prepareColumn(const QString &first,
const QString &second,
- const QString &third )
+ const QString &third)
{
QList<QStandardItem *> colItems;
colItems << new QStandardItem(first);
@@ -79,4 +79,4 @@ QList<QStandardItem *> ModelView::prepareColumn(const QString &first,
colItems << new QStandardItem(third);
return colItems;
}
-//! [Quoting ModelView Tutorial] \ No newline at end of file
+//! [Quoting ModelView Tutorial]
diff --git a/examples/tutorials/modelview/6_treeview/modelview.h b/examples/tutorials/modelview/6_treeview/modelview.h
index 2329e89..a47111c 100755
--- a/examples/tutorials/modelview/6_treeview/modelview.h
+++ b/examples/tutorials/modelview/6_treeview/modelview.h
@@ -56,7 +56,7 @@ private:
QStandardItemModel *standardModel;
QList<QStandardItem *> prepareColumn(const QString &first,
const QString &second,
- const QString &third );
+ const QString &third);
public:
ModelView(QWidget *parent = 0);
};
diff --git a/examples/tutorials/modelview/7_selections/modelview.cpp b/examples/tutorials/modelview/7_selections/modelview.cpp
index 2ef980d..3b373c6 100755
--- a/examples/tutorials/modelview/7_selections/modelview.cpp
+++ b/examples/tutorials/modelview/7_selections/modelview.cpp
@@ -74,13 +74,13 @@ ModelView::ModelView(QWidget *parent)
italyItem-> appendRow(veronaItem);
//register the model
- treeView->setModel( standardModel );
+ treeView->setModel(standardModel);
treeView->expandAll();
//selection changes shall trigger a slot
QItemSelectionModel *selectionModel= treeView->selectionModel();
- connect(selectionModel, SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
- this, SLOT(selectionChangedSlot(const QItemSelection & , const QItemSelection & )));
+ connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
+ this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
}
//! [quoting modelview_a]
@@ -93,7 +93,7 @@ void ModelView::selectionChangedSlot(const QItemSelection & /*newSelection*/, co
QString selectedText = index.data(Qt::DisplayRole).toString();
int hierarchyLevel=1;
QModelIndex seekRoot = index;
- while(seekRoot.parent() != QModelIndex() )
+ while(seekRoot.parent() != QModelIndex())
{
seekRoot = seekRoot.parent();
hierarchyLevel++;
diff --git a/examples/tutorials/modelview/qmake.pro b/examples/tutorials/modelview/qmake.pro
deleted file mode 100755
index 7f684ba..0000000
--- a/examples/tutorials/modelview/qmake.pro
+++ /dev/null
@@ -1,10 +0,0 @@
-TEMPLATE = subdirs
-
-SUBDIRS = 1_readonly \
- 2_formatting \
- 3_changingmodel \
- 4_headers \
- 5_edit \
- 6_treeview \
- 7_selections
-