Discussion:
[Interest] QVariant toString locale
Etienne Sandré-Chardonnal
2018-10-17 14:10:04 UTC
Permalink
Dear all,

I have a big table made of QVariants of many subtypes (dates, floats,
integers, etc...)
I need to output it in csv format with a user defined locale.

It seems, after searching the documentation for all possible ways:
1) QTextStream can be set a locale, but it does not accepts QVariant
2) QVariant has a toString method, but no custom locale can be used. The
documentation says nothing about the locale it uses, probably
QLocale::system() or QApplication::locale but maybe not.

So, I have to either:
1) Handle all possible QVariant subtypes in a big switch block, I'd like
to avoid...
2) Figure out if QVariant uses QApplication::locale(); change this locale,
do all the conversion, and restore QApplication::locale(), but this could
cause GUI flickers (as the GUI uses the app locale for display) and I would
rely on an undocumented assumption...

So as you see, it seems there is a hole in Qt's otherwise excellent
handling of locales. Am I wrong? Is there a good solution?

Best regards,

Etienne
Thiago Macieira
2018-10-17 15:10:47 UTC
Permalink
Post by Etienne Sandré-Chardonnal
1) Handle all possible QVariant subtypes in a big switch block, I'd like
to avoid...
So as you see, it seems there is a hole in Qt's otherwise excellent
handling of locales. Am I wrong? Is there a good solution?
Yeah, but our handling of QVariant is nowhere as good. That big switch is
something we've also tried to avoid, but to the point that we know it's
lacking and improperly done. There are a few updates for 5.12 that remove some
of the old code, but nothing that would help you.

The light at the end of the tunnel is that you don't need to handle all types,
only those that do get affected by the locale. That's usually those containing
floating point. I'd handle those first in your conversion and everything else
simply pass to QVariant::toString
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Loading...