qt-interest@trolltech.com
[Top] [All Lists]

Re: Changing Alignment of QTableItem with Localized Number format

Subject: Re: Changing Alignment of QTableItem with Localized Number format
From: Dimitri
Date: Mon, 25 Oct 2004 11:50:38 +0200
Hi,

[...] If 100000 is not a number shouldn't QString::number produce localized output? I am using %L trick to achieve it.

You're absolutely right about this. Both QString::number() and QString::toInt() are supposed to understand the current locale, at least according to the source code:

        QString QString::number( int n, int base )
        {
            QString s;
            s.setNum( n, base );
            return s;
        }

        QString &QString::setNum( Q_LLONG n, int base )
        {
            [...]
            QLocale locale(QLocale::C);
            *this = locale.d->longLongToString(n, -1, base);
            return *this;
        }

        int QString::toInt( bool *ok, int base ) const
        {
            Q_LLONG v = toLongLong( ok, base );
            [...]
        }

        Q_LLONG QString::toLongLong( bool *ok, int base ) const
        {
            [...]
            QLocale def_locale;
            Q_LLONG result = def_locale.d->stringToLongLong(...);
            [...]
            // If the default was not "C", try the "C" locale
            [...]
            QLocale c_locale(QLocale::C);
            return c_locale.d->stringToLongLong(...);
        }

If one of these functions fails for you, then it's probably a Qt bug.

I've written the following test case and run it on Linux:

$ cat > foo.cc
#include <qstring.h>
int main() {
    int i = 100000;
    qDebug(QString::number(i).latin1());
    bool ok;
    QString us("100,000");
    qDebug("%s -> %d (%s)", us.latin1(), us.toInt(&ok),
      ok ? "OK" : "ERROR");
    QString eu("100000");
    qDebug("%s -> %d (%s)", eu.latin1(), eu.toInt(&ok),
      ok ? "OK" : "ERROR");
}
$ g++ -I$QTDIR/include -o foo foo.cc -L$QTDIR/lib -lqt-mt
$ env LANG=en_US ./foo
100000
0 - OK
100000 - ERROR
$ env LANG=fr_FR ./foo
100000
0 - OK
100000 - ERROR
$

There are many issues here:
1) The documentation does not state clearly whether the QString conversion functions understand the current locale or not. 2) The "ok" return value should be set to FALSE in case of a conversion error, and to TRUE otherwise. This doesn't seem to be the case. 3) These functions don't seem to be understanding the current locale, at least on my Fedora 2 system.

Which version of Qt are you using? On which system?

--
Dimitri

--
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>