Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-78094

`locale` object in ``QStyledItemDelegate::displayText()` does not actually use locale settings for formatting

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.13.0
    • None
    • Qt 5.13.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 9.1.1 20190805 [gcc-9-branch revision 274114]) on "xcb"

      OS: openSUSE Tumbleweed [linux version 5.2.10-1-default]
    • Linux/X11

    Description

      Brief description of the buggy behaviour

      The locale argument passed to the displayText() method for QStyledItemDelegate objects doesn’t work properly, i.e. the toString() method for the locale doesn’t actually use the locale settings for formatting.

      An example

      I’ll attach a short, self contained example project showing the bug. Basically, the project has a simple QTableView containing a single cell with the number 123456789 in it. The displayText method for the QStyledItemDelegate has been written to format the cell content using the default toString() method for the locale passed, but with a * suffix:

      QString MyDelegate::displayText(const QVariant & value, const QLocale & locale) const {
          return locale.toString(value.toInt()) + "*";
      }

      Expected behaviour

      For my locale (nn_NO, i.e. Norwegian Nynorsk in Norway), this means that the number 123456789 should be formatted as ‘123 456 789*’ (since nn_NO uses a space as the thousands separator).

      Actual behaviour

      The number is formatted as a ‘123456789*’ (like in a C locale).

      Wrong locale or locale settings?

      So one would think the bug is that either that 1) the locale object just contains the wrong locale (a C locale), or 2) that the nn_NO locale doesn’t actually use space as a separator. However, 1) the name of locale is nn_NO, and 2) using an explicit QLocale("nn_NO") instead of locale does correctly format the number.

      Setting the (default) locale

      The locale (nn_NO) has been set to Norwegian Nynorsk (Norway) in main(), using

      QLocale::setDefault(QLocale(QLocale::NorwegianNynorsk, QLocale::Norway));
      

      And for good measure, the LANGUAGE, LC_ALL and LANG variables have all been set to Norwegian Nynorsk before starting the application:

      export LANGUAGE=nn:no:nb:da:en_GB:en@quot
      export LC_ALL=nn_NO.utf8
      export LANG=nn_NO.utf8
      

      What works and what doesn’t

      In the attached project, I have also added debug messages for various ways of selecting a default, system or manually selected locale for formatting:

      qWarning() << "Passed locale: " << locale.name() << " Formatting: " << locale.toString(value.toInt());
       qWarning() << "Manual locale: " << QLocale(QLocale::NorwegianNynorsk, QLocale::Norway).name() << " Formatting: " << QLocale(QLocale::NorwegianNynorsk, QLocale::Norway).toString(value.toInt());
       qWarning() << "Manual locale V2: " << QLocale("nn_NO").name() << " Formatting: " << QLocale("nn_NO").toString(value.toInt());
       qWarning() << "System locale: " << QLocale::system().name() << " Formatting: " << QLocale::system().toString(value.toInt());
       qWarning() << "Default locale: " << QLocale().name() << " Formatting: " << QLocale().toString(value.toInt());
      

      All of them except the one using locale passed to displayText() work fine:

      Passed locale:    "nn_NO"  Formatting: "123456789" 
      Manual locale:    "nn_NO"  Formatting: "123 456 789" 
      Manual locale V2: "nn_NO"  Formatting: "123 456 789" 
      System locale:    "nn_NO"  Formatting: "123 456 789" 
      Default locale:   "nn_NO"  Formatting: "123 456 789"

      Changing the default locale as set in main() from Norwegian Nynorsk to German (which uses a dot as the thousands separator) results in:

      QLocale::setDefault(QLocale(QLocale::German, QLocale::Germany));
      
      Passed locale:    "de_DE"  Formatting: "123456789" 
      Manual locale:    "nn_NO"  Formatting: "123 456 789" 
      Manual locale V2: "nn_NO"  Formatting: "123 456 789" 
      System locale:    "nn_NO"  Formatting: "123 456 789" 
      Default locale:   "de_DE"  Formatting: "123.456.789"

      Note that the names (de_DE or nn_NO) are all what they should be, and the formatting is also correct for all except the first one, i.e. the one using locale argument passed. The strange thing is that the formatting is wrong, but locale name is correct!

      Additional information

      While the bug is for a custom displayText, using the default one results in similar behaviour, i.e. the number 123456789 is formatted as 123456789 instead of 123 456 789.

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            huftis Karl Ove Hufthammer
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes