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

Add QString::toStdU8String and QString::fromStdU8String

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.15.6, 6.0.1, 6.1.3, 6.2.0
    • None
    • All

    Description

      Qt already has functions to convert from and to std::u16string / std::u32string.
      Since c++20 and c23, we have char8_t. It would be good, to also have functions which convert from/to std::u8string.

      QString::toStdU8String is missing and should look like this for pre c++20:

      inline std::string QString::toStdU8String () const
      { return toUtf8().toStdString(); }
      

      for c++20:
      QString::toStdU8String is missing and should look like this:

      inline std::string QString::toStdU8String () const
      { return toUtf8().toStdU8String(); }
      

      Optional:
      I also noticed that the `QString::toStdString` function is ambiguous: C++ does not specify an encoding for it, but it is only used together with the locale encoding. This makes the function unusable in cases where the `std::locale` is not based on UTF-8. This is particularly fatal in China, where UTF-8 is practically forbidden as the default on desktops (even on UNIX Systems). But also on Windows computers world wide, which typically do not use utf-8 as default, instead ANSI is used.

      After the discussion below, I won't propose anymore to deprecate it, but rather to also add `QString::from/toLocalStdString`.

      Attachments

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

        Activity

          People

            Unassigned Unassigned
            febbe Fabian Keßler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes