Does anyone know why big int values on QVariant store as double ?



  • Does anyone know why big int values on QVariant store as double ?



  • I'm not sure what do you mean but QVariant works fine with maximum value of int

    @#include <QCoreApplication>
    #include <QDebug>
    #include <limits>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    int x = INT_MAX;
    qDebug() << QVariant(x).toInt();
    
    return a.exec&#40;&#41;;
    

    }
    @

    You can past a snippet in case that's not what you mean.



  • Try to use it this way:

    @
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    int x = INT_MAX;
    QVariant v1 = x;
    qDebug() << v1;
    
    return a.exec&#40;&#41;;
    

    }

    @


  • Moderators

    this outputs the following in the console for me:
    [quote]QVariant(int, 2147483647)[/quote]

    Which compiler are you using?



  • [quote author="aabc" date="1367143234"]Try to use it this way:[/quote]

    It works fine to me!!! I got this:

    bq. QVariant(int, 2147483647)



  • It happens when I use it with QML (Created C++ QML object)


  • Moderators

    [quote author="aabc" date="1367143234"]Try to use it this way:

    @
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    int x = INT_MAX;
    QVariant v1 = x;
    qDebug() << v1;
    
    return a.exec&#40;&#41;;
    

    }
    @
    [/quote]

    well..whats your output of these lines?!



  • The same as yours.
    This problem happens when I use it with QML.
    For some reason if I pass large int to variant property (C++ QVariant) it stores it as double



  • QML/JavaScript only has a single number type, implemented as a double. Assigning directly to a QVariant property will keep this type information. Is this what you are seeing?


  • Moderators

    show the code please.



  • Not exactly.
    Whan I assign to QML int property small value (such as 500) the C++ QVariant stores it as int.
    But when I assign to this QML int property large value (such as 1111111111) the C++ QVariant stores it as double.



  • C++:

    @
    class MyItem: public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QVariant value       READ value          WRITE setValue  NOTIFY valueChanged)
    

    public:
    inline const QVariant& value() const
    {
    return m_value;
    }

    inline void setValue(const QVariant& _value)
    {
        qDebug() << "_value = " << _value;
        qDebug() << "m_value = " << m_value;
    
        if(_value != m_value)
        {
            m_value = _value;
            emit valueChanged();
        }
    }
    

    private:
    QVariant m_value;
    }
    @

    QML:

    @
    property int myQmlInt: 111111111

    MyItem
    {
    id: myItem1
    value: myQmlInt
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.