Skip to content

Commit 7d8fba8

Browse files
committedSep 27, 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.
1 parent 35b3b79 commit 7d8fba8

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

@@ -65,6 +68,33 @@ def test_SetValue(self):
6568
self.doAttributeTest(0, ['value', '123', NULL, NULL])
6669
self.doAttributeTest(1, [NULL, 123, NULL, NULL])
6770

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

0 commit comments

Comments
 (0)
Please sign in to comment.