Results 1 to 6 of 6

Thread: Locale and database

  1. #1
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Locale and database

    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

  2. #2
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Locale and database

    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)

  3. #3
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Locale and database

    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?

  4. #4
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Locale and database

    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;
    }

  5. #5
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Locale and database

    I use QT 4.6, which one do you use?

  6. #6
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Locale and database

    4.7.4, that must be why

Similar Threads

  1. How to change locale
    By memus in forum Newbie
    Replies: 5
    Last Post: 29th May 2011, 12:50
  2. About locale issue
    By xiajingan in forum Qt Programming
    Replies: 3
    Last Post: 26th February 2009, 21:18
  3. QDate an locale setting
    By Boron in forum Qt Programming
    Replies: 5
    Last Post: 10th October 2008, 15:15
  4. Setting locale for the whole app
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2008, 22:53
  5. Qt4 and system locale
    By L.Marvell in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2007, 13:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.