diff --git a/src/gui/editorwidgets/qgstexteditwrapper.cpp b/src/gui/editorwidgets/qgstexteditwrapper.cpp index 8283c0121de4..ececb9ca159b 100644 --- a/src/gui/editorwidgets/qgstexteditwrapper.cpp +++ b/src/gui/editorwidgets/qgstexteditwrapper.cpp @@ -58,9 +58,14 @@ QVariant QgsTextEditWrapper::value() const v = mLineEdit->text(); } - if ( ( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) || - v == QgsApplication::nullRepresentation() ) + if ( ( v.isEmpty() && ( field().type() == QVariant::Int + || field().type() == QVariant::Double + || field().type() == QVariant::LongLong + || field().type() == QVariant::Date ) ) + || v == QgsApplication::nullRepresentation() ) + { return QVariant( field().type() ); + } if ( !defaultValue().isNull() && v == defaultValue().toString() ) { @@ -323,9 +328,14 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val ) v = v.remove( QLocale().groupSeparator() ); } - if ( mTextEdit ) + const QVariant currentValue { value( ) }; + // Note: comparing QVariants leads to funny (and wrong) results: + // QVariant(0.0) == QVariant(QVariant.Double) -> True + const bool changed { val != currentValue || val.isNull() != currentValue.isNull() }; + + if ( changed ) { - if ( val != value() ) + if ( mTextEdit ) { if ( config( QStringLiteral( "UseHtml" ) ).toBool() ) { @@ -337,18 +347,19 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val ) } } else + { mTextEdit->setPlainText( v ); + } } - } - - if ( mPlainTextEdit ) - { - if ( val != value() ) + else if ( mPlainTextEdit ) + { mPlainTextEdit->setPlainText( v ); + } + else if ( mLineEdit ) + { + mLineEdit->setText( v ); + } } - - if ( mLineEdit && val != value() ) - mLineEdit->setText( v ); } void QgsTextEditWrapper::setHint( const QString &hintText ) diff --git a/tests/src/gui/testqgsattributeform.cpp b/tests/src/gui/testqgsattributeform.cpp index 102e6ec87b41..23eafc55efac 100644 --- a/tests/src/gui/testqgsattributeform.cpp +++ b/tests/src/gui/testqgsattributeform.cpp @@ -54,6 +54,7 @@ class TestQgsAttributeForm : public QObject void testDefaultValueUpdate(); void testDefaultValueUpdateRecursion(); void testSameFieldSync(); + void testZeroDoubles(); private: QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww ) @@ -1109,5 +1110,20 @@ void TestQgsAttributeForm::testSameFieldSync() QCOMPARE( les[1]->cursorPosition(), 4 ); } +void TestQgsAttributeForm::testZeroDoubles() +{ + // See issue GH #34118 + QString def = QStringLiteral( "Point?field=col0:double" ); + QgsVectorLayer layer { def, QStringLiteral( "test" ), QStringLiteral( "memory" ) }; + layer.setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() ) ); + QgsFeature ft( layer.dataProvider()->fields(), 1 ); + ft.setAttribute( QStringLiteral( "col0" ), 0.0 ); + QgsAttributeForm form( &layer ); + form.setFeature( ft ); + QList les = form.findChildren( "col0" ); + QCOMPARE( les.count(), 1 ); + QCOMPARE( les.at( 0 )->text(), QStringLiteral( "0" ) ); +} + QGSTEST_MAIN( TestQgsAttributeForm ) #include "testqgsattributeform.moc"