Skip to content
Permalink
Browse files

Fix editing of synchronized same fields in attribute form

  • Loading branch information
github-actions authored and nyalldawson committed Jan 8, 2020
1 parent 8713d96 commit 86ed380e1c5753f31cb86712758eaca071017aba
Showing with 43 additions and 1 deletion.
  1. +1 −1 src/gui/editorwidgets/qgstexteditwrapper.cpp
  2. +42 −0 tests/src/gui/testqgsattributeform.cpp
@@ -271,7 +271,7 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
mPlainTextEdit->setPlainText( v );
}

if ( mLineEdit )
if ( mLineEdit && val != value() )
mLineEdit->setText( v );
}

@@ -53,6 +53,7 @@ class TestQgsAttributeForm : public QObject
void testAttributeFormInterface();
void testDefaultValueUpdate();
void testDefaultValueUpdateRecursion();
void testSameFieldSync();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
@@ -1067,5 +1068,46 @@ void TestQgsAttributeForm::testDefaultValueUpdateRecursion()
QCOMPARE( ww3->value().toInt(), 43 );
}

void TestQgsAttributeForm::testSameFieldSync()
{
// Check that widget synchronisation works when a form contains the same field several times
// and there is no issues when editing

// make a temporary vector layer
QString def = QStringLiteral( "Point?field=col0:integer" );
QgsVectorLayer *layer = new QgsVectorLayer( def, QStringLiteral( "test" ), QStringLiteral( "memory" ) );
layer->setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() ) );

// add a feature to the vector layer
QgsFeature ft( layer->dataProvider()->fields(), 1 );
ft.setAttribute( QStringLiteral( "col0" ), 10 );

// add same field twice so they get synced
QgsEditFormConfig editFormConfig = layer->editFormConfig();
editFormConfig.clearTabs();
editFormConfig.addTab( new QgsAttributeEditorField( "col0", 0, editFormConfig.invisibleRootContainer() ) );
editFormConfig.addTab( new QgsAttributeEditorField( "col0", 0, editFormConfig.invisibleRootContainer() ) );
editFormConfig.setLayout( QgsEditFormConfig::TabLayout );
layer->setEditFormConfig( editFormConfig );

layer->startEditing();

// build a form for this feature
QgsAttributeForm form( layer );
form.setFeature( ft );

QList<QLineEdit *> les = form.findChildren<QLineEdit *>( "col0" );
QCOMPARE( les.count(), 2 );

les[0]->setCursorPosition( 1 );
QTest::keyClick( les[0], Qt::Key_2 );
QTest::keyClick( les[0], Qt::Key_3 );

QCOMPARE( les[0]->text(), QString( "1230" ) );
QCOMPARE( les[0]->cursorPosition(), 3 );
QCOMPARE( les[1]->text(), QString( "1230" ) );
QCOMPARE( les[1]->cursorPosition(), 4 );
}

QGSTEST_MAIN( TestQgsAttributeForm )
#include "testqgsattributeform.moc"

0 comments on commit 86ed380

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