Skip to content

Commit

Permalink
Ensure that dataChanged signal is raised for DisplayRole in
Browse files Browse the repository at this point in the history
QgsAttributeTableModel when a feature attribute is changed

Otherwise the attribute table will show the outdated value
until it is repainted
  • Loading branch information
nyalldawson committed Dec 8, 2022
1 parent 9480c64 commit 3458bea
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -364,7 +364,11 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
if ( mFeatureRequest.filterType() == QgsFeatureRequest::FilterNone )
{
if ( loadFeatureAtId( fid ) )
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
{
const QModelIndex modelIndex = index( idToRow( fid ), fieldCol( idx ) );
setData( modelIndex, value, Qt::EditRole );
emit dataChanged( modelIndex, modelIndex, QVector<int>() << Qt::DisplayRole );
}
}
else
{
Expand All @@ -380,7 +384,9 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
else
{
// Update representation
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
const QModelIndex modelIndex = index( idToRow( fid ), fieldCol( idx ) );
setData( modelIndex, value, Qt::EditRole );
emit dataChanged( modelIndex, modelIndex, QVector<int>() << Qt::DisplayRole );
}
}
else
Expand Down
60 changes: 60 additions & 0 deletions tests/src/python/test_qgsattributetablemodel.py
Expand Up @@ -31,9 +31,12 @@
QgsField,
QgsFields,
QgsWkbTypes,
QgsFeatureRequest
)
from qgis.PyQt.QtCore import Qt, QTemporaryDir, QVariant
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtTest import QSignalSpy

from qgis.testing import (start_app,
unittest
)
Expand Down Expand Up @@ -116,7 +119,19 @@ def testEdit(self):

# change attribute value for a feature and commit
self.layer.startEditing()

spy = QSignalSpy(self.am.dataChanged)

self.layer.changeAttributeValue(fid, field_idx, new_value)

# ensure that dataChanged signal was raised
self.assertEqual(len(spy), 1)
self.assertEqual(spy[-1][0].row(), model_index.row())
self.assertEqual(spy[-1][0].column(), field_idx)
self.assertEqual(spy[-1][1].row(), model_index.row())
self.assertEqual(spy[-1][1].column(), field_idx)
self.assertEqual(spy[-1][2], [Qt.DisplayRole])

self.layer.commitChanges()

# check the feature in layer is good
Expand All @@ -130,6 +145,51 @@ def testEdit(self):
# check that index from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

def testEditWithFilter(self):
fid = 2
field_idx = 1
new_value = 334

# get the same feature from model and layer
feature = self.layer.getFeature(fid)
am = QgsAttributeTableModel(self.cache)
am.setRequest(QgsFeatureRequest().setFilterFid(fid))
am.loadLayer()

model_index = am.idToIndex(fid)
feature_model = am.feature(model_index)

# check that feature from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

# change attribute value for a feature and commit
self.layer.startEditing()

spy = QSignalSpy(am.dataChanged)

self.layer.changeAttributeValue(fid, field_idx, new_value)

# ensure that dataChanged signal was raised
self.assertEqual(len(spy), 1)
self.assertEqual(spy[-1][0].row(), model_index.row())
self.assertEqual(spy[-1][0].column(), field_idx)
self.assertEqual(spy[-1][1].row(), model_index.row())
self.assertEqual(spy[-1][1].column(), field_idx)
self.assertEqual(spy[-1][2], [Qt.DisplayRole])

self.layer.commitChanges()

# check the feature in layer is good
feature = self.layer.getFeature(fid)
self.assertEqual(feature.attribute(field_idx), new_value)

# get the same feature from model and layer
model_index = am.idToIndex(fid)
feature_model = am.feature(model_index)

# check that index from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

def testStyle(self):
style_threshold = 2
color = QColor(133, 133, 133)
Expand Down

0 comments on commit 3458bea

Please sign in to comment.