/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\page qtquick-modelviewsdata-cppmodels.html
\title Using C++ Models with QtQuick Views
\brief using QtQuick views with models defined in C++


\section1 Data Provided In A Custom C++ Model

Models can be defined in C++ and then made available to QML. This is useful
for exposing existing C++ data models or otherwise complex datasets to QML.

A C++ model class can be defined as a \l QStringList, a QObjectList or a
\l QAbstractItemModel. The first two are useful for exposing simpler datasets,
while QAbstractItemModel provides a more flexible solution for more complex
models.

\section2 QStringList-based Model

A model may be a simple \l QStringList, which provides the contents of the list
via the \e modelData role.

Here is a ListView with a delegate that references its model item's
value using the \c modelData role:

\snippet quick/models/stringlistmodel/view.qml 0

A Qt application can load this QML document and set the value of \c myModel
to a QStringList:

\snippet quick/models/stringlistmodel/main.cpp 0

The complete source code for this example is available in
\l {quick/modelviews/stringlistmodel}{examples/quick/modelviews/stringlistmodel}
within the Qt install directory.

\b{Note:} There is no way for the view to know that the contents of a QStringList
have changed.  If the QStringList changes, it will be necessary to reset
the model by calling QQmlContext::setContextProperty() again.


\section2 QObjectList-based model

A list of QObject* values can also be used as a model. A QList<QObject*> provides
the properties of the objects in the list as roles.

The following application creates a \c DataObject class with
Q_PROPERTY values that will be accessible as named roles when a
QList<DataObject*> is exposed to QML:

\snippet quick/models/objectlistmodel/dataobject.h 0
\dots 4
\snippet quick/models/objectlistmodel/dataobject.h 1
\codeline
\snippet quick/models/objectlistmodel/main.cpp 0
\dots

The QObject* is available as the \c modelData property.  As a convenience,
the properties of the object are also made available directly in the
delegate's context. Here, \c view.qml references the \c DataModel properties in
the ListView delegate:

\snippet quick/models/objectlistmodel/view.qml 0

Note the use of \c color property with qualifier.
The properties of the object are not replicated in the \c model
object, as they are easily available via the \c modelData
object.

The complete source code for this example is available in
\l {quick/modelviews/objectlistmodel}{examples/quick/modelviews/objectlistmodel}
within the Qt install directory.

Note: There is no way for the view to know that the contents of a QList
has changed.  If the QList changes, it is necessary to reset
the model by calling QQmlContext::setContextProperty() again.


\section2 QAbstractItemModel

A model can be defined by subclassing QAbstractItemModel. This is the
best approach if you have a more complex model that cannot be supported
by the other approaches. A QAbstractItemModel can also automatically
notify a QML view when the model data changes.

The roles of a QAbstractItemModel subclass can be exposed to QML by
reimplementing QAbstractItemModel::roleNames(). The default role names
set by Qt are:

\table
\header
\li Qt Role
\li QML Role Name
\row
\li Qt::DisplayRole
\li display
\row
\li Qt::DecorationRole
\li decoration
\endtable

Here is an application with a QAbstractListModel subclass named \c AnimalModel,
which exposes the \e type and \e sizes roles. It reimplements
QAbstractItemModel::roleNames() to expose the role names, so that they can be
accessed via QML:

\snippet quick/models/abstractitemmodel/model.h 0
\dots
\snippet quick/models/abstractitemmodel/model.h 1
\dots
\snippet quick/models/abstractitemmodel/model.h 2
\codeline
\snippet quick/models/abstractitemmodel/model.cpp 0
\codeline
\snippet quick/models/abstractitemmodel/main.cpp 0
\dots

This model is displayed by a ListView delegate that accesses the \e type and \e size
roles:

\snippet quick/models/abstractitemmodel/view.qml 0

QML views are automatically updated when the model changes. Remember the model
must follow the standard rules for model changes and notify the view when
the model has changed by using QAbstractItemModel::dataChanged(),
QAbstractItemModel::beginInsertRows(), and so on. See the \l {Model subclassing reference} for
more information.

The complete source code for this example is available in
\l {quick/modelviews/abstractitemmodel}{examples/quick/modelviews/abstractitemmodel}
within the Qt install directory.

QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML
can only display list data.
In order to display the child lists of a hierarchical model,
use the VisualDataModel type, which provides the following properties and functions to be used
with list models of QAbstractItemModel type:

\list
\li \e hasModelChildren role property to determine whether a node has child nodes.
\li \l VisualDataModel::rootIndex allows the root node to be specified
\li \l VisualDataModel::modelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
\li \l VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex
\endlist

\section2 Exposing C++ Data Models to QML

The above examples use QQmlContext::setContextProperty() to set
model values directly in QML components. An alternative to this is to
register the C++ model class as a QML type (either
\l{qtqml-registercpptypes.html}{directly} from a C++ entry-point, or within
the initialization function of a \l{qtqml-modules-cppplugins.html}
{QML C++ plugin}, as shown below).  This would allow the model classes to be
created directly as elements within QML:

\table
\row

\li
\code
class MyModelPlugin : public QQmlExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.QmlExtension.MyModel" FILE "mymodel.json")
public:
    void registerTypes(const char *uri)
    {
        qmlRegisterType<MyModel>(uri, 1, 0,
                "MyModel");
    }
}
\endcode

\li
\qml
MyModel {
    id: myModel
    ListElement { someProperty: "some value" }
}
\endqml

\qml
ListView {
    width: 200; height: 250
    model: myModel
    delegate: Text { text: someProperty }
}
\endqml

\endtable

See \l {Tutorial: Extending QML with C++} for details on writing QML C++
plugins.


*/


