Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-4053

QVariant should not mistake 0 or "" for null.

XMLWordPrintable

      Summary should be pretty self explanatory I guess.
      The problem is that when the type is declared, the object is initialized. The == operator should take isNull() into account.

      here's a testcase that illustrates the problem:

      #include <QtTest/QtTest>
      #include <QVariant>
      
      
      class Test : public QObject{
            Q_OBJECT
                      private slots:
      
      
      
            void testNullVariant_data(){
      
                  QTest::addColumn<int>("type");
      
      
                  QTest::newRow("double") << int(QVariant::Double);
                  QTest::newRow("int")    << int(QVariant::Int);
                  QTest::newRow("bool")   << int(QVariant::Bool);
                  QTest::newRow("uint")   << int(QVariant::UInt);
                  QTest::newRow("ulonglong") << int(QVariant::ULongLong);
                  QTest::newRow("longlong")  << int(QVariant::LongLong);
      
            }
      
      
            void testNullVariant(){
      
                  QVariant v1(0);
                  QFETCH(int, type);
                  QVariant v2 = QVariant((QVariant::Type)type);
      
                  QVERIFY(!v1.isNull());
                  QVERIFY(v2.isNull());
                  QEXPECT_FAIL("", "are not equal", Abort);
                  QCOMPARE(v1,v2);
            }
      
            void testEmptyStringVariant(){
                  QVariant v1("");
                  QVariant v2 = QVariant(QVariant::String);
      
                  QVERIFY(!v1.isNull());
                  QVERIFY(v2.isNull());
                  QEXPECT_FAIL("", "are not equal", Abort);
                  QCOMPARE(v1,v2);
            }
      
      };
      
      
      QTEST_MAIN(Test)
      #include "test.moc"
      
      

      NB: Since this could break compatibility behaviour, it might not be changed in a very near future.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            stormols Marius Storm-Olsen
            ntg Pierre Rossi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes