Davor Josipovic Just another WordPress blog – rather tryout

11/05/2010

IsDirty() class member for QDataWidgetMapper

Filed under: C++,Programming,Qt — Tags: , — Davor @ 15:57

Here is something I wanted to share. It’s a pity something similar isn’t implemented by the Trolls. But then again, it doesn’t require much coding either.

    bool isDirty() const {
        Q_ASSERT(orientation() == Qt::Horizontal);
        Q_ASSERT(rootIndex() == QModelIndex());
        for(int i = 0; i < model()->columnCount(); i++) {
            QWidget *mapWidget = mappedWidgetAt(i);
            if (mapWidget){
                QByteArray p = mappedPropertyName(mapWidget);
                QModelIndex idx = model()->index(currentIndex(), i);
                if (idx.data(Qt::EditRole) != mapWidget->property(p))
                    return true;
            }
        }
        return false;
    }

One use is to check whether an update is required before going to the next record…

PS Other way one could achieve the same thing by adjusting the source code because the “mappings”-container is in the private implementation of QDataWidgetMapper.

1 Comment »

  1. Hi,

    I suggest this code instead (with a more intensive memory usage):

    /**
     * class Patients::Internal::IsDirtyDataWidgetMapper
     * The data mapper keeps the model original value when you set its current index,
     * then you can use the isDirty() method to make the comparison between cached original
     * values and the widgets current value. n
     * Note that when the model is submitted, you have to refresh the cache with onModelSubmitted(). n
     * This make no sense to use this data mapper with a submit policy (setSubmitPolicy())
     * different from QDataWidgetMapper::ManualSubmit.
    */
    class IsDirtyDataWidgetMapper: public QDataWidgetMapper
    {
        Q_OBJECT
    public:
        IsDirtyDataWidgetMapper(QObject *parent = 0) :
            QDataWidgetMapper(parent)
        {}
     
        /** Use this method each time the model gets correctly submitted */
        void onModelSubmitted()
        {
            refreshCache();
        }
     
        /** Return true if the current values of the mapped widget differs from the original model values */
        bool isDirty() const
        {
            Q_ASSERT(orientation() == Qt::Horizontal);
            Q_ASSERT(rootIndex() == QModelIndex());
     
            // cycle through all widgets the mapper supports
            for(int i = 0; i columnCount(); i++) {
                QWidget *mapWidget = mappedWidgetAt(i);
                if (mapWidget) {
                    const QVariant &amp;current = mapWidget-&gt;property(mappedPropertyName(mapWidget));
                    const QVariant &amp;orig = _original.value(mapWidget);
     
                    // Special case of null original variant
                    if (orig.isNull() &amp;&amp; current.toString().isEmpty())
                        continue;
                    if (current != orig) {
                        qWarning() &lt;&lt; &quot;DIRTY row found:&quot;
                                   &lt;&lt; &quot;orig&quot; &lt;&lt; orig
                                   &lt;&lt; &quot;current&quot; &lt;&lt; current;
                        return true;
                    }
                }
            }
            return false;
        }
     
    public Q_SLOTS:
        /** Overload method (creates the internal cache) */
        void setCurrentIndex(int index)
        {
            refreshCache();
            QDataWidgetMapper::setCurrentIndex(index);
        }
     
    private:
        void refreshCache()
        {
            Q_ASSERT(orientation() == Qt::Horizontal);
            Q_ASSERT(rootIndex() == QModelIndex());
            _original.clear();
            // cycle through all widgets the mapper supports
            for(int i = 0; i columnCount(); i++) {
                QWidget *mapWidget = mappedWidgetAt(i);
                if (mapWidget) {
                    _original.insert(mapWidget, model()-&gt;data(model()-&gt;index(currentIndex(), i)));
                }
            }
        }
     
    private:
        QHash _original;
    };

    Comment by ericmaeker — 08/12/2012 @ 00:06

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress