Skip to content
Permalink
Browse files

Fix widget wrong values 0 doubles

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 Jan 30, 2020
1 parent 282f90e commit bdafd7b4149490d608d144f414d1757038740805
Showing with 39 additions and 12 deletions.
  1. +23 −12 src/gui/editorwidgets/qgstexteditwrapper.cpp
  2. +16 −0 tests/src/gui/testqgsattributeform.cpp
@@ -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() )
{
@@ -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() )
{
@@ -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 )
@@ -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<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.
You can’t perform that action at this time.