summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorMichael D Scull <ext-michael.scull@nokia.com>2010-06-28 10:49:05 (GMT)
committerDavid Boddie <dboddie@trolltech.com>2010-07-07 15:10:17 (GMT)
commit798433c9046281a8739eb6b313c7dd0fc7b5e3b1 (patch)
tree583cd879dcfd575ef6fbe52199795f51aa69f8ad /doc
parentca10c2c5a8190cac9c2ae0526f2e250add09d6f2 (diff)
downloadQt-798433c9046281a8739eb6b313c7dd0fc7b5e3b1.zip
Qt-798433c9046281a8739eb6b313c7dd0fc7b5e3b1.tar.gz
Qt-798433c9046281a8739eb6b313c7dd0fc7b5e3b1.tar.bz2
I've cleaned up the qdoc file a bit.
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/src/tutorials/modelview.qdoc213
1 files changed, 68 insertions, 145 deletions
diff --git a/doc/src/tutorials/modelview.qdoc b/doc/src/tutorials/modelview.qdoc
index 3c73a80..c9caf17 100755
--- a/doc/src/tutorials/modelview.qdoc
+++ b/doc/src/tutorials/modelview.qdoc
@@ -56,9 +56,13 @@ Here is an overview of the model/view widgets and their corresponding standard w
\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.
-\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 \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 \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
\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.
@@ -71,7 +75,7 @@ We start with an application that uses a \l QTableView to show data. We will add
-------------------------------------------------------------main.cpp---------------------
\snippet examples/tutorials/modelview/1_readonly/main.cpp Quoting ModelView Tutorial
-We have the usual main() function;
+We have the usual \l {tutorials/modelview/1_readonly/main.cpp}{main()} function;
-------------------------------------------------------------modelview.h---------------------
\snippet examples/tutorials/modelview/1_readonly/modelview.h Quoting ModelView Tutorial
@@ -80,7 +84,7 @@ 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 \c tableView->setModel(new MyModel(this) ); to instantiate the Model and pass its pointer to \l {QTableView::}{tableView()} OR \l QTableView::tableView() OR \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 \c 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.
@@ -154,54 +158,17 @@ Now we need to determine how using a seperated model impacts the application's p
We still have a read only table, but this time the content changes every second because we are showing the current time.
-!!!!!I CAN'T FIND THIS FILE!!!!!
-\code
-QVariant MyModel::data(const QModelIndex &index, int role ) const
-{
- QVariant returnVal;
- int row = index.row();
- int col = index.column();
-
- if(role == Qt::DisplayRole)
-
- {
- if(row == 0 && col == 0 )
- {
- returnVal = QTime::currentTime().toString();
- }
- }
- return returnVal;
-}
-\endcode
+ \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.
-\code
-MyModel::MyModel(QObject *parent)
- :QAbstractTableModel(parent)
-{
-// selectedCell = 0;
- timer = new QTimer(this);
- timer->setInterval(1000);
- connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()) );
- timer->start();
-}
-\endcode
+ \snippet examples/tutorials/modelview/3_changingmodel/mymodel.cpp quoting mymodel_a
Here is the corresponding slot:
-\code
+ \snippet examples/tutorials/modelview/3_changingmodel/mymodel.cpp quoting mymodel_b
-void MyModel::timerHit()
-{
- //we identify the top left cell
- QModelIndex topLeft = createIndex ( 0,0 );
- //emit a signal to make the view reread identified data
- emit dataChanged ( topLeft, topLeft );
-}
-\endcode
-
-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{QAbstractItemModel::setModel()}{setModel()} .
+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()} .
\section2 2.4 Setting up Headers for Columns and Rows
Headers can be hidden via a view method.
@@ -209,28 +176,9 @@ Headers can be hidden via a view method.
\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:
-\code
-QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
-{
- if (role == Qt::DisplayRole)
- {
- if (orientation == Qt::Horizontal) {
- switch (section)
- {
- case 0:
- return QString("first");
- case 1:
- return QString("second");
- case 2:
- return QString("third");
- }
- }
- }
- return QVariant();
-}
-\endcode
+ \snippet examples/tutorials/modelview/4_headers/mymodel.cpp quoting mymodel_c
\section2 2.5 The minimal Editing example
@@ -242,19 +190,7 @@ The model decides whether editing capabilities are available . We only have to m
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.
-\code
-#include "mymodel.h"
-
-const int COLS= 3;
-const int ROWS= 2;
-
-MyModel::MyModel(QObject *parent)
- :QAbstractTableModel(parent)
-{
- //gridData needs to have 6 element, one for each table cell
- m_gridData << "1/1" << "1/2" << "1/3" << "2/1" << "2/2" << "2/3" ;
-}
-\endcode
+ \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)
\snippet examples/tutorials/modelview/5_edit/mymodel.cpp quoting mymodel_e
@@ -266,6 +202,7 @@ Various properties of a cell can be adjusted with \l{QAbstractItemModel::flags()
\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
@@ -273,76 +210,33 @@ You can convert the example above into an application with a tree view. Simply r
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
-We want to, however, 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 QStandardItems, which are able to hold all the standard properties of items like text, fonts, checkboxes or brushes. \image tree_2_with_algorithm.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---------------------
\snippet examples/tutorials/modelview/6_treeview/modelview.cpp Quoting ModelView Tutorial
-We simply instantiate a QStandardItemModel and add a couple of QStandardItems to the constructor. We can then make a hierarchical data structure because a QStandardItem can hold other QStandardItems. Nodes are collapsed and expanded within the view.
+We simply instantiate a QStandardItemModel and add a couple of 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
+
+
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:
-\code
-#include <QTreeView>
-#include <QStandardItemModel>
-#include <QItemSelectionModel>
-#include "modelview.h"
-
-ModelView::ModelView(QWidget *parent)
- : QMainWindow(parent)
-{
- treeView = new QTreeView(this);
- setCentralWidget(treeView);
- standardModel = new QStandardItemModel ;
- QStandardItem *rootNode = standardModel->invisibleRootItem();
-
-
- //defining a couple of items
- QStandardItem *americaItem = new QStandardItem("America");
- QStandardItem *mexicoItem = new QStandardItem("Canada");
- QStandardItem *usaItem = new QStandardItem("USA");
- QStandardItem *bostonItem = new QStandardItem("Boston");
- QStandardItem *europeItem = new QStandardItem("Europe");
- QStandardItem *italyItem = new QStandardItem("Italy");
- QStandardItem *romeItem = new QStandardItem("Rome");
- QStandardItem *veronaItem = new QStandardItem("Verona");
-
- //building up the hierarchy
- rootNode-> appendRow(americaItem);
- rootNode-> appendRow(europeItem);
- americaItem-> appendRow(mexicoItem);
- americaItem-> appendRow(usaItem);
- usaItem-> appendRow(bostonItem);
- europeItem-> appendRow(italyItem);
- italyItem-> appendRow(romeItem);
- italyItem-> appendRow(veronaItem);
-
- //register the model
- 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 & )));
-}
-\endcode
-
+ \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{QAbstractItemModel::selectionModel()}{selectionModel()} method. We retrieve the selection Model in order to connect a slot to its \l{QAbstractItemModel::selectionChanged()}{selectionChanged()} signal.
\snippet examples/tutorials/modelview/7_selections/modelview.cpp quoting modelview_b
We get the model index that corresponds to the selection by calling
-\c 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 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.
+\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.
-The selection model (as shown above) can be retrieved, but it can also be set with \c 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{QAbstractItemModel::selectionModel()}{selectionModel()} and the result is assigned to the second and third view class with \l{QAbstractItemModel::setSelectionModel()}{setSelectionModel()};
+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{QAbstractItemModel::selectionModel()}{selectionModel()} and the result is assigned to the second and third view class with \l{QAbstractItemModel::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.
@@ -354,9 +248,12 @@ 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 {1, 2} QFileSystemModel
-QDirModel (deprecated)
- \o {1, 2} Encapsulate the local file system
+ \o QFileSystemModel
+ \raw HTML
+ <br>
+ \endraw
+ QDirModel (deprecated)
+ \o Encapsulate the local file system
\row
\o QSqlQueryModel
\o Encapsulate an SQL result set
@@ -375,9 +272,8 @@ QDirModel (deprecated)
\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{QAbstractItemModel::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.
+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.
-!!!!!I CAN'T FIND THIS FILE!!!!!
\code
class StarDelegate : public QStyledItemDelegate
{
@@ -392,20 +288,22 @@ The view has a method that replaces the default delegate and installs a custom d
\endcode
-\l{QAbstractItemModel::paint()}{paint()} draws stars depending on the content of the underlying data. The data can be looked up with parameter \c index.data(). \c SizeHint specifies the stars dimensions so the the cell will provide enough height and width to accommodate the stars.
+\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{QStyledItemDelegate::SizeHint}{SizeHint} specifies the stars dimensions so the the cell will provide enough height and width to accommodate the stars.
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.
\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.
-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. http://labs.qt.nokia.com/page/Projects/Itemview/Modeltest
+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
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.
-
+\raw HTML
+<br>
+\endraw
@@ -413,25 +311,50 @@ Unlike view classes, ModelTest uses out of range indexes to test the model. Thi
\section2 3.6 Model/View NG
-\image path.png
+\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
-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
+<!-- wrap content table p has 0 padding and the padding for p outside of the table is 5px-->
+\endraw
+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><td align="right" valign="top">
+\endraw
+
+\inlineimage path.png
+
+\raw HTML
+</td></tr></table>
+\endraw
+\raw HTML
+<br>
+\endraw
\section1 4 Good Sources for Additional Information
\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 1.C++ GUI Programming with Qt 4 / Jasmin Blanchette, Mark Summerfield, Prentice Hall, 2nd edition, ISBN 0-13-235416-0
+ \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 1.The Book of Qt4, The Art of Building Qt Applications / Daniel Molkentin, Open Source Press ISBN 1-59327-147-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 1.Foundations of Qt Development / Johan Thelin, Apress, ISBN 1-59059-831-8
+ \o Foundations of Qt Development / Johan Thelin, 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.