Important: Please read the Qt Code of Conduct -

[5.5.1] QVariant Support for Long Double

  • Hi,

    I recently discovered that QVariant does not support the long double format. The source code I'm using requires the use of the QVariant class for data types. I am dealing with numbers that go down to the 10^-9 degree, so using floats or ordinary doubles won't help me much with precision. Can anyone help me out with one of the following things:

    1. If someone has already solved this problem, can that someone forward the code to me?
    2. A workaround to this problem that does not instigate a loss of data precision.
    3. Can someone instruct me (or point me in the right direction) on how to modify the QVariant class so that is has long double support (or educate me on the inner workings of the class)?


  • Moderators

    QVariant supports any type of data. You don't need to modify the class. You just need to register your type.

    In some header register the type:

    Q_DECLARE_METATYPE(long double)

    and then you can use it:

    long double foo = 42.0;
    QVariant bar = QVariant::fromValue(foo);     //put value into variant
    long double bazz = bar.value<long double>(); //and get it back

    If you also want to serialize/deserialize your variants you'll need to implement and register stream operators for it: qRegisterMetaTypeStreamOperators().

Log in to reply