PDA

View Full Version : Locale and database



lynnH
24th February 2012, 09:24
I am querying a mysql database and retrieving float information. As specify in "Data Types for Qt-supported Database Systems" my floats get mapped to strings.

My problem is that the string format is the French one (my system is in French) and I want it in English
QVariant(QString, "395,134") et no QVariant(QString, "395.134")

I tried to change the locale in the main class before launching the main gui thread without result. The default locale is from there on correctly set to en_US but this has no effect on the data retrieved from the database.

Any help on how to achieve this would be greatly appreciated.

Lynn

Le_B
24th February 2012, 09:31
QVariant v("3.14");
qDebug() << v.toDouble(); //3.14
QLocale fr(QLocale::French);
qDebug() << fr.toString(v.toDouble()); //3,14

(sorry it s inverted but you got the solution)

lynnH
24th February 2012, 10:17
Thanks for the reply.

There is something weird. If I set the default local to en_US and then try the conversion it does not work:

QLocale::setDefault(QLocale::English);
QLocale vDefaultLocal = QLocale();
kDebug()<<"vDefaultLocal "<<vDefaultLocal.name(); //en_US
bool ok;
QVariant v("3,14");
QLocale vLocal = QLocale(QLocale::English);
kDebug()<<"local system "<<vLocal.name(); //en_US
double vDouble = v.toDouble(&ok);
kDebug()<<" vLocal double "<<vLocal.toString(vDouble) //"0"
kDebug()<<" vDefaultLocal double "<<vDefaultLocal.toString(vDouble); //"0"

If I remove the set default local, it works:

Debug()<<"vDefaultLocal "<<vDefaultLocal.name(); //fr_FR
kDebug()<<"local system "<<vLocal.name(); //en_US
...
kDebug()<<" vLocal double "<<vLocal.toString(vDouble) // "3.14"
kDebug()<<" vDefaultLocal double "<<vDefaultLocal.toString(vDouble); // "3,14"

=> something with the QLocale::setDefault function?

My question is: how can I get QVariant(QString, "395.134") and no QVariant(QString, "395,134") directly out of the database?

Le_B
24th February 2012, 14:17
strange, i have a french system too and if i run this it works:

{
QString str = "3,14";
QVariant v(str.toDouble());

qDebug() << str; //"3,14"
qDebug() << v; //QVariant(QString, "3.14")

return 0;
}

lynnH
24th February 2012, 16:39
I use QT 4.6, which one do you use?

Le_B
24th February 2012, 16:40
4.7.4, that must be why