mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-28 00:17:30 -05:00
Merge pull request #6615 from elpaso/bugfix-16967-multi-in-value-relation
[bugfix] value relation widget with Allow multiple selection doesn't resolve
This commit is contained in:
commit
685adbf7e1
@ -63,6 +63,17 @@ This can be used to keep the value map in the local memory
|
||||
if doing multiple lookups in a loop.
|
||||
|
||||
.. versionadded:: 3.0
|
||||
%End
|
||||
|
||||
static QStringList valueToStringList( const QVariant &value );
|
||||
%Docstring
|
||||
Utility to convert an array or a string representation of and array ``value`` to a string list
|
||||
|
||||
:param value: The value to be converted
|
||||
|
||||
:return: A string list
|
||||
|
||||
.. versionadded:: 3.2
|
||||
%End
|
||||
};
|
||||
|
||||
|
@ -54,7 +54,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer *layer, i
|
||||
|
||||
if ( config.value( QStringLiteral( "AllowMulti" ) ).toBool() )
|
||||
{
|
||||
QStringList keyList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
|
||||
QStringList keyList = valueToStringList( value );
|
||||
QStringList valueList;
|
||||
|
||||
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( vrCache ) )
|
||||
@ -142,3 +142,23 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
QStringList QgsValueRelationFieldFormatter::valueToStringList( const QVariant &value )
|
||||
{
|
||||
QStringList checkList;
|
||||
if ( value.type() == QVariant::StringList )
|
||||
checkList = value.toStringList();
|
||||
else if ( value.type() == QVariant::String )
|
||||
checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
|
||||
else if ( value.type() == QVariant::List )
|
||||
{
|
||||
QVariantList valuesList( value.toList( ) );
|
||||
for ( const QVariant &listItem : qgis::as_const( valuesList ) )
|
||||
{
|
||||
QString v( listItem.toString( ) );
|
||||
if ( ! v.isEmpty() )
|
||||
checkList.append( v );
|
||||
}
|
||||
}
|
||||
return checkList;
|
||||
}
|
||||
|
@ -70,6 +70,15 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
|
||||
* \since QGIS 3.0
|
||||
*/
|
||||
static QgsValueRelationFieldFormatter::ValueRelationCache createCache( const QVariantMap &config );
|
||||
|
||||
/**
|
||||
* Utility to convert an array or a string representation of and array \a value to a string list
|
||||
*
|
||||
* \param value The value to be converted
|
||||
* \return A string list
|
||||
* \since QGIS 3.2
|
||||
*/
|
||||
static QStringList valueToStringList( const QVariant &value );
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
|
||||
|
@ -151,11 +151,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
|
||||
{
|
||||
if ( mListWidget )
|
||||
{
|
||||
QStringList checkList;
|
||||
if ( value.type() == QVariant::StringList )
|
||||
checkList = value.toStringList();
|
||||
else if ( value.type() == QVariant::String )
|
||||
checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
|
||||
QStringList checkList( QgsValueRelationFieldFormatter::valueToStringList( value ) );
|
||||
|
||||
for ( int i = 0; i < mListWidget->count(); ++i )
|
||||
{
|
||||
|
@ -107,6 +107,16 @@ class TestQgsValueRelationFieldFormatter(unittest.TestCase):
|
||||
|
||||
QgsProject.instance().removeMapLayer(second_layer.id())
|
||||
|
||||
def test_valueToStringList(self):
|
||||
|
||||
def _test(a, b):
|
||||
self.assertEqual(QgsValueRelationFieldFormatter.valueToStringList(a), b)
|
||||
|
||||
_test([1, 2, 3], ["1", "2", "3"])
|
||||
_test("{1,2,3}", ["1", "2", "3"])
|
||||
_test(['1', '2', '3'], ["1", "2", "3"])
|
||||
_test('not an array', ['not an array'])
|
||||
|
||||
|
||||
class TestQgsRelationReferenceFieldFormatter(unittest.TestCase):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user