Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, May 2008
QVariant(QVariant::Int) == QVariant(0)


Message 1 in thread

$SUBJ.

~/src :) cat qvariant.cpp
#include <QtCore/QVariant>
#include <iostream>

int main()
{
    QVariant q1(QVariant::Int), q2(0);

    std::cout << (q1 == q2) << std::endl;
}

~/src :) g++ -lQtCore -I/usr/include/qt4 -o qvariant qvariant.cpp
~/src :) ./qvariant
1

Is this really the intent? All other Variants (most notably SQL ones) compare 
unequal when one is NULL and the other isnât.

-- 
 [ signature omitted ] 

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Message 2 in thread

On Tuesday 27 May 2008 11:15:18 Roman Odaisky wrote:
> $SUBJ.
>
> ~/src :) cat qvariant.cpp
> #include <QtCore/QVariant>
> #include <iostream>
>
> int main()
> {
>     QVariant q1(QVariant::Int), q2(0);
>
>     std::cout << (q1 == q2) << std::endl;
> }
>
> ~/src :) g++ -lQtCore -I/usr/include/qt4 -o qvariant qvariant.cpp
> ~/src :) ./qvariant
> 1
>
> Is this really the intent? All other Variants (most notably SQL ones)
> compare unequal when one is NULL and the other isnât.

Yes, that's the intent here. You're mistaking isNull() for isValid() in 
QVariant.

$ cat main.cpp
#include <QtCore/QVariant>
#include <iostream>

int main()
{
    QVariant q1(QVariant::Int), q2(0);
    QVariant q3(QVariant::Invalid), q4;

    std::cout << q1.type() << q2.type()
              << q3.type() << q4.type() << std::endl;
    std::cout << q1.isNull() << q2.isNull()
              << q3.isNull() << q4.isNull() << std::endl;
    std::cout << q1.isValid() << q2.isValid()
              << q3.isValid() << q4.isValid() << std::endl;
}

$ ./qvariant
2200
1011
1100

QVariant's concept of null descends into the type. For example, null QStrings 
and null QByteArrays, when placed inside QVariant, make it a null QVariant. 
But it's still a valid variant, containing a QString.

Also note that a null QString compares equal to an empty-but-not-null QString. 
So the behaviour of QVariant is also consistent to the rest of Qt.
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Very nicely explained, this should be added to the docs! 

Cheers,
Peter

Thiago Macieira wrote:
> $ cat main.cpp
> #include <QtCore/QVariant>
> #include <iostream>
> 
> int main()
> {
>     QVariant q1(QVariant::Int), q2(0);
>     QVariant q3(QVariant::Invalid), q4;
> 
>     std::cout << q1.type() << q2.type()
>               << q3.type() << q4.type() << std::endl;
>     std::cout << q1.isNull() << q2.isNull()
>               << q3.isNull() << q4.isNull() << std::endl;
>     std::cout << q1.isValid() << q2.isValid()
>               << q3.isValid() << q4.isValid() << std::endl;
> }
> 
> $ ./qvariant
> 2200
> 1011
> 1100
> 
> QVariant's concept of null descends into the type. For 
> example, null QStrings 
> and null QByteArrays, when placed inside QVariant, make it a 
> null QVariant. 
> But it's still a valid variant, containing a QString.
> 
> Also note that a null QString compares equal to an 
> empty-but-not-null QString. 
> So the behaviour of QVariant is also consistent to the rest of Qt.

--
 [ signature omitted ] 

Message 4 in thread

On Tuesday, 27.05.2008 13:17:49 Peter Prade wrote:

> Very nicely explained, this should be added to the docs!

+1. The Qt docs are sometimes lacking in subtle details such as this.

However, my concern is the following. I have a field in a DB which can be 0, 
1, or NULL. I want to display it in a QComboBox, but the only way seems to be 
a custom delegate (otherwise NULL and 0 get mixed). I resorted to this:

if(comboBox->property("mustSetIndex").toBool())
{
    QVariant const qv = index.model()->data(index);
    
    int ix;
    for(ix = 0; ix < comboBox->count(); ++ix)
    {
        QVariant const qvCurr = comboBox->itemData(ix);
        if(qvCurr.isNull() == qv.isNull() && qvCurr == qv)
        {
            break;
        }
    }
    
    comboBox->setCurrentIndex(ix);
    return;
}

Abominable, but works.

-- 
 [ signature omitted ] 

Attachment: smime.p7s
Description: S/MIME cryptographic signature