Skip to content
Permalink
Browse files

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)
  • Loading branch information
nyalldawson committed Oct 6, 2016
1 parent c2fbab1 commit 83d5719c46a9bb372ab1c6c97abf9b813f2ee922
Showing with 41 additions and 1 deletion.
  1. +10 −0 src/gui/editorwidgets/qgstexteditwrapper.cpp
  2. +31 −1 tests/src/python/test_qgseditwidgets.py
@@ -66,9 +66,19 @@ QVariant QgsTextEditWrapper::value() const

QVariant res( v );
if ( field().convertCompatible( res ) )
{
return res;
}
else if ( field().type() == QVariant::String && field().length() > 0 )
{
// for string fields convertCompatible may return false due to field length limit - in this case just truncate
// input rather then discarding it entirely
return QVariant( v.left( field().length() ) );
}
else
{
return QVariant( field().type() );
}
}

QWidget* QgsTextEditWrapper::createWidget( QWidget* parent )
@@ -14,10 +14,13 @@

import qgis # NOQA

from qgis.core import QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL
from qgis.core import (QgsMapLayerRegistry, QgsFeature, QgsGeometry, QgsPoint, QgsProject, QgsRelation, QgsVectorLayer, NULL,
QgsField)
from qgis.gui import QgsEditorWidgetRegistry

from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtWidgets import QTextEdit

start_app()

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

def testStringWithMaxLen(self):
""" tests that text edit wrappers correctly handle string fields with a maximum length """
layer = QgsVectorLayer("none?field=fldint:integer", "layer", "memory")
assert layer.isValid()
layer.dataProvider().addAttributes([QgsField('max', QVariant.String, 'string', 10),
QgsField('nomax', QVariant.String, 'string', 0)])
layer.updateFields()
QgsMapLayerRegistry.instance().addMapLayer(layer)

reg = QgsEditorWidgetRegistry.instance()
config = {'IsMultiline': 'True'}

# first test for field without character limit
editor = QTextEdit()
editor.setPlainText('this_is_a_long_string')
w = reg.create('TextEdit', layer, 2, config, editor, None)
self.assertEqual(w.value(), 'this_is_a_long_string')

# next test for field with character limit
editor = QTextEdit()
editor.setPlainText('this_is_a_long_string')
w = reg.create('TextEdit', layer, 1, config, editor, None)

self.assertEqual(w.value(), 'this_is_a_')

QgsMapLayerRegistry.instance().removeAllMapLayers()

def test_ValueRelation_representValue(self):

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

0 comments on commit 83d5719

Please sign in to comment.
You can’t perform that action at this time.