Skip to content

Commit

Permalink
Fix editing of synchronized same fields in attribute form
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Jan 8, 2020
1 parent 8713d96 commit 86ed380
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgstexteditwrapper.cpp
Expand Up @@ -271,7 +271,7 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
mPlainTextEdit->setPlainText( v );
}

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

Expand Down
42 changes: 42 additions & 0 deletions tests/src/gui/testqgsattributeform.cpp
Expand Up @@ -53,6 +53,7 @@ class TestQgsAttributeForm : public QObject
void testAttributeFormInterface();
void testDefaultValueUpdate();
void testDefaultValueUpdateRecursion();
void testSameFieldSync();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
Expand Down Expand Up @@ -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.