Skip to content

Commit 83d5719

Browse files
committedOct 6, 2016
Preserve edits for multiline editor when length exceeds field size
Previously when using the multiline option for text edit widgets the entire contents of the field would be discarded if the entered value exceeded the maximum length for a string field. Now the entered string is truncated to the maximum field length. (cherry-picked from 7d8fba8)
1 parent c2fbab1 commit 83d5719

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

‎src/gui/editorwidgets/qgstexteditwrapper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,19 @@ QVariant QgsTextEditWrapper::value() const
6666

6767
QVariant res( v );
6868
if ( field().convertCompatible( res ) )
69+
{
6970
return res;
71+
}
72+
else if ( field().type() == QVariant::String && field().length() > 0 )
73+
{
74+
// for string fields convertCompatible may return false due to field length limit - in this case just truncate
75+
// input rather then discarding it entirely
76+
return QVariant( v.left( field().length() ) );
77+
}
7078
else
79+
{
7180
return QVariant( field().type() );
81+
}
7282
}
7383

7484
QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )

‎tests/src/python/test_qgseditwidgets.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515
import qgis # NOQA
1616

17-
from qgis.core import QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL
17+
from qgis.core import (QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL,
18+
QgsField)
1819
from qgis.gui import QgsEditorWidgetRegistry
1920

2021
from qgis.testing import start_app, unittest
22+
from qgis.PyQt.QtCore import QVariant
23+
from qgis.PyQt.QtWidgets import QTextEdit
2124

2225
start_app()
2326

@@ -63,6 +66,33 @@ def test_SetValue(self):
6366
self.doAttributeTest(0, ['value', '123', NULL, NULL])
6467
self.doAttributeTest(1, [NULL, 123, NULL, NULL])
6568

69+
def testStringWithMaxLen(self):
70+
""" tests that text edit wrappers correctly handle string fields with a maximum length """
71+
layer = QgsVectorLayer("none?field=fldint:integer", "layer", "memory")
72+
assert layer.isValid()
73+
layer.dataProvider().addAttributes([QgsField('max', QVariant.String, 'string', 10),
74+
QgsField('nomax', QVariant.String, 'string', 0)])
75+
layer.updateFields()
76+
QgsMapLayerRegistry.instance().addMapLayer(layer)
77+
78+
reg = QgsEditorWidgetRegistry.instance()
79+
config = {'IsMultiline': 'True'}
80+
81+
# first test for field without character limit
82+
editor = QTextEdit()
83+
editor.setPlainText('this_is_a_long_string')
84+
w = reg.create('TextEdit', layer, 2, config, editor, None)
85+
self.assertEqual(w.value(), 'this_is_a_long_string')
86+
87+
# next test for field with character limit
88+
editor = QTextEdit()
89+
editor.setPlainText('this_is_a_long_string')
90+
w = reg.create('TextEdit', layer, 1, config, editor, None)
91+
92+
self.assertEqual(w.value(), 'this_is_a_')
93+
94+
QgsMapLayerRegistry.instance().removeAllMapLayers()
95+
6696
def test_ValueRelation_representValue(self):
6797

6898
first_layer = QgsVectorLayer("none?field=foreign_key:integer",

0 commit comments

Comments
 (0)
Please sign in to comment.