<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to ModelViews</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>Recent changes to ModelViews</description><atom:link href="https://sourceforge.net/p/startkladde/wiki/ModelViews/feed" rel="self"/><language>en</language><lastBuildDate>Sat, 10 Nov 2012 22:38:51 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/startkladde/wiki/ModelViews/feed" rel="self" type="application/rss+xml"/><item><title>WikiPage ModelViews modified by Martin Herrmann</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>&lt;pre&gt;--- v4
+++ v5
@@ -57,7 +57,7 @@
 
 You may have to reset internal data and/or refresh the view. You can probably use the same methods called on model reset for this.
 
-In a proxy model, the process must be wrapped in calls to `beginResetModel ()` (must be called before resetting internal data) and `endResetModel ()`. You can probably use the methods connected to the source model's signals with the same names.
+In a proxy model, the process of resetting internal data must be wrapped in calls to `beginResetModel ()` and `endResetModel ()`. You can probably use the methods connected to the source model's signals with the same names.
 
 
 ## Reacting to model deletion
&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin Herrmann</dc:creator><pubDate>Sat, 10 Nov 2012 22:38:51 -0000</pubDate><guid>https://sourceforge.net863d8d2bfe6d4518542648a98c4c4b5d2ab5418c</guid></item><item><title>WikiPage ModelViews modified by Martin Herrmann</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>&lt;pre&gt;--- v3
+++ v4
@@ -57,7 +57,7 @@
 
 You may have to reset internal data and/or refresh the view. You can probably use the same methods called on model reset for this.
 
-In a proxy model, the process must be wrapped in calls to `beginResetModel ()` (must be called before resetting internal data) and `endResetModel ()`.
+In a proxy model, the process must be wrapped in calls to `beginResetModel ()` (must be called before resetting internal data) and `endResetModel ()`. You can probably use the methods connected to the source model's signals with the same names.
 
 
 ## Reacting to model deletion
&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin Herrmann</dc:creator><pubDate>Sat, 10 Nov 2012 22:34:19 -0000</pubDate><guid>https://sourceforge.netdab74d517750517de71afa23cdca422049cee512</guid></item><item><title>WikiPage ModelViews modified by Martin Herrmann</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>&lt;pre&gt;--- v2
+++ v3
@@ -55,7 +55,9 @@
                  this       , SLOT   (modelDestroyed ()));
     }
 
-Refresh the view, if necessary.
+You may have to reset internal data and/or refresh the view. You can probably use the same methods called on model reset for this.
+
+In a proxy model, the process must be wrapped in calls to `beginResetModel ()` (must be called before resetting internal data) and `endResetModel ()`.
 
 
 ## Reacting to model deletion
&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin Herrmann</dc:creator><pubDate>Sat, 10 Nov 2012 22:31:00 -0000</pubDate><guid>https://sourceforge.net27d0cfb50b87ee21d6cbd6ec8137c6c80b6efdd8</guid></item><item><title>WikiPage ModelViews modified by Martin Herrmann</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>&lt;pre&gt;--- v1
+++ v2
@@ -27,6 +27,10 @@
     :::C++
     if (this-&gt;model)
     {
+        // Disconnect all signals of the model from this view
+        _sourceModel-&gt;disconnect (this);
+
+        // Alternatively, you can disconnect the signals individually:
         disconnect (this-&gt;model, SIGNAL (dataChanged (QModelIndex, QModelIndex)),
                     this       , SLOT   (dataChanged (QModelIndex, QModelIndex)));
         // ...
@@ -34,7 +38,7 @@
                     this       , SLOT   (modelDestroyed ()));
     }
 
-Store the current model to the new model:
+Store the the new model:
 
     :::C++
     this-&gt;model=model;
&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin Herrmann</dc:creator><pubDate>Sat, 10 Nov 2012 22:16:14 -0000</pubDate><guid>https://sourceforge.net885bc3eb1c6fb04a4637d1878696ed2ae6ab39ff</guid></item><item><title>WikiPage ModelViews modified by Martin Herrmann</title><link>https://sourceforge.net/p/startkladde/wiki/ModelViews/</link><description>Several classes act as model views or something similar. An example for a true model view is `FlarmMapWidget` which is a view for `FlarmList` and `GpsTracker`. An example that is not strictly a model view but uses similar techniques is `GpsTracker` which uses an `NmeaDecoder`.

This page describes the techniques employed by these clases.


## General

The model may be `NULL`. Make sure that all methods accessing the model handle this case correctly.

An alternative technique, used by Qt's own model views, is to use a (static) null object.


## Setting the model

The model is set by a method called `setModel` or something more specific (especially for classes using several models) like `setGpsTracker`.

This method performes the following steps:

Check if the new model is the same as the current model:

    :::C++
    if (model==this-&gt;model)
        return;

Disconnect the signals of the old model, if there is an old model:

    :::C++
    if (this-&gt;model)
    {
        disconnect (this-&gt;model, SIGNAL (dataChanged (QModelIndex, QModelIndex)),
                    this       , SLOT   (dataChanged (QModelIndex, QModelIndex)));
        // ...
        disconnect (this-&gt;model, SIGNAL (destroyed()),
                    this       , SLOT   (modelDestroyed ()));
    }

Store the current model to the new model:

    :::C++
    this-&gt;model=model;

If the new model is valid (it may be NULL), connect its signals. Pay particular attention to the `destroyed()` signal:

    :::C++
    if (this-&gt;model)
    {
        connect (this-&gt;model, SIGNAL (dataChanged (QModelIndex, QModelIndex)),
                 this       , SLOT   (dataChanged (QModelIndex, QModelIndex)));
        // ...
        connect (this-&gt;model, SIGNAL (destroyed()),
                 this       , SLOT   (modelDestroyed ()));
    }

Refresh the view, if necessary.


## Reacting to model deletion

In the slot connected to the model's `destroyed()` signal, set the current model to `NULL`:

    :::C++
    this-&gt;model=NULL;

Do not call `setModel (NULL)` - this will try to disconnect the signals which are no longer valid at this point.

You will probably want to refresh the view after that; if the model is a QAbstractItemModel, you can probably use the method connected to the model's `modelReset` slot.
</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Martin Herrmann</dc:creator><pubDate>Tue, 11 Sep 2012 18:56:15 -0000</pubDate><guid>https://sourceforge.neta4adb39b84cf7b4b4ed7715982e8f2717db1ad13</guid></item></channel></rss>