Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel setText using a utf-8 string is rendered with Latin1 encoding
QtWS25 Last Chance

QLabel setText using a utf-8 string is rendered with Latin1 encoding

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 882 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jessevg
    wrote on last edited by Jessevg
    #1

    This problem presents itself on our embedded device, if we pass utf-8 strings to the setText of a QLabel it will render for example ° as 2 Latin1 characters, however if I make a small gui on my desktop and do setText there with a utf-8 string it will show the ° sign correctly.

    There must be some way for Qt to detect the kind of text encoding used or a way to set it, but I can't figure it out, anyone knows what's up here?

    The embedded version uses Qt 6.4.0 and sets a fixed font to DejaVu Sans from here: https://github.com/dejavu-fonts/dejavu-fonts

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      Which Qt version are you targetting?

      Director R&D, The Qt Company

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jessevg
        wrote on last edited by
        #3

        Desktop version that uses utf8 correctly is Qt 6.3.0/6.3.2, on Windows as well as my Ubuntu this works, our embedded version is 6.4.0 (plain embedded Linux built with buildroot) I can provide the GUI/Widgets related cmake config options for our embedded Qt build if this would help

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So in your non-working environment - does your compiler there use utf-8 as input encoding and is the locale also utf-8?

          Rule of thumb: don't use non-ascii chars in code, use QTranslator.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          J 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            So in your non-working environment - does your compiler there use utf-8 as input encoding and is the locale also utf-8?

            Rule of thumb: don't use non-ascii chars in code, use QTranslator.

            J Offline
            J Offline
            Jessevg
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            The locale I'm not sure, it's in buildroot, but I'll put it on the mailing list if someone there can help me along.
            As for the actual source file this originates from, everything worked when the file was encoded as ISO 8859-1, but I removed some code there, which forced an UTF-8 change as our source control system expects every new/changed file to be UTF-8 as a new standard, and that's when things started to break, compilation is standard gcc cross compilation, and this one seems to do things correct, as it generates the correct char array I provide to my QString, I did check that, it's just passing that QString to the gui element that I need to be able to do with UTF-8

            1 Reply Last reply
            0
            • kkoehneK Offline
              kkoehneK Offline
              kkoehne
              Moderators
              wrote on last edited by kkoehne
              #6

              From Qt 6 onwards, Qt treats all source code as UTF-8. And QString internally has always used UTF-16. So if you have checked that the QString actually has the correct content, it's most likely a font rendering problem.

              But have you really double-checked the contents of the QString, e.g. with

              QChar *data = str.data();
              while (!data->isNull()) {
                  qDebug() << data->unicode();
                  ++data;
              }
              

              ?

              Director R&D, The Qt Company

              J 1 Reply Last reply
              1
              • kkoehneK kkoehne

                From Qt 6 onwards, Qt treats all source code as UTF-8. And QString internally has always used UTF-16. So if you have checked that the QString actually has the correct content, it's most likely a font rendering problem.

                But have you really double-checked the contents of the QString, e.g. with

                QChar *data = str.data();
                while (!data->isNull()) {
                    qDebug() << data->unicode();
                    ++data;
                }
                

                ?

                J Offline
                J Offline
                Jessevg
                wrote on last edited by
                #7

                @kkoehne

                That was a good tip, I was just looking at raw data but my unicode char was split up into 2 QChars, explaining the double chars I saw.
                This however led me to the real culprit of my troubles, QString::fromLocal8bit.

                The incorrect string came from such a call with a char array, and the documentation led me to believe this was fully equivalent to a QString::fromUtf8 call as it states "On Unix systems this is equivalen to fromUtf8(), on Windows the systems current code page is being used."

                However looking at the implementation this doesn't seem to be the case because QStringDecoder with System encoding is used which again states it'll be UTF-8 on a unix system, but this doesn't seem to be the case for me at first glance and I can't immediately find it in the implementation either.

                I will investigate further and provide an appropriate bug report if needed depending on what I find.

                Thanks for the help.

                Christian EhrlicherC 1 Reply Last reply
                2
                • J Jessevg

                  @kkoehne

                  That was a good tip, I was just looking at raw data but my unicode char was split up into 2 QChars, explaining the double chars I saw.
                  This however led me to the real culprit of my troubles, QString::fromLocal8bit.

                  The incorrect string came from such a call with a char array, and the documentation led me to believe this was fully equivalent to a QString::fromUtf8 call as it states "On Unix systems this is equivalen to fromUtf8(), on Windows the systems current code page is being used."

                  However looking at the implementation this doesn't seem to be the case because QStringDecoder with System encoding is used which again states it'll be UTF-8 on a unix system, but this doesn't seem to be the case for me at first glance and I can't immediately find it in the implementation either.

                  I will investigate further and provide an appropriate bug report if needed depending on what I find.

                  Thanks for the help.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Jessevg said in QLabel setText using a utf-8 string is rendered with Latin1 encoding:

                  QStringDecoder with System encoding is used which again states it'll be UTF-8 on a unix system

                  QStringDecoder::System always assumes that it's utf-8, see qstringconvert_p.h / QLocal8Bit implementation for non-windows builds. Therefore when your locale on your target system is not utf-8 the stuff will get messed up.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved