Skip to content

Commit

Permalink
Fix widget wrong values 0 doubles
Browse files Browse the repository at this point in the history
Fixes #34118

Because:

QVariant(0.0) == QVariant(QVariant.Double) -> True

but:

QVariant(0.0).isNull() == QVariant(QVariant.Double).isNull() -> False
  • Loading branch information
elpaso authored and nyalldawson committed Feb 5, 2020
1 parent 282f90e commit bdafd7b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/gui/editorwidgets/qgstexteditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,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() )
{
Expand Down Expand Up @@ -247,9 +252,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() )
{
Expand All @@ -261,18 +271,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 )
Expand Down
16 changes: 16 additions & 0 deletions tests/src/gui/testqgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TestQgsAttributeForm : public QObject
void testDefaultValueUpdate();
void testDefaultValueUpdateRecursion();
void testSameFieldSync();
void testZeroDoubles();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
Expand Down Expand Up @@ -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<QLineEdit *> les = form.findChildren<QLineEdit *>( "col0" );
QCOMPARE( les.count(), 1 );
QCOMPARE( les.at( 0 )->text(), QStringLiteral( "0" ) );
}

QGSTEST_MAIN( TestQgsAttributeForm )
#include "testqgsattributeform.moc"

0 comments on commit bdafd7b

Please sign in to comment.