Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qml can not show string encoded by utf-8?
Forum Updated to NodeBB v4.3 + New Features

Qml can not show string encoded by utf-8?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 2.9k Views 2 Watching
  • 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.
  • C Offline
    C Offline
    Crawl.W
    wrote on 19 Nov 2020, 05:51 last edited by
    #1
    Text {
        text: "\xE2\x84\x83"
        font.pixelSize: 24
    }
    

    "\xE2\x84\x83" is the UTF-8 encoding for Celsius. It shows as gabberish characters in qml, normal in c++.
    My platform is Windows 10, MSVC2019 with Qt 5.15.0 64bit.

    K 1 Reply Last reply 19 Nov 2020, 07:17
    0
    • C Crawl.W
      19 Nov 2020, 05:51
      Text {
          text: "\xE2\x84\x83"
          font.pixelSize: 24
      }
      

      "\xE2\x84\x83" is the UTF-8 encoding for Celsius. It shows as gabberish characters in qml, normal in c++.
      My platform is Windows 10, MSVC2019 with Qt 5.15.0 64bit.

      K Offline
      K Offline
      KroMignon
      wrote on 19 Nov 2020, 07:17 last edited by KroMignon
      #2

      @Crawl-W I think you should use the unicode notation \u2103 (works for me ™)

      Text {
          text: "\u2103"
          font.pixelSize: 24
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      C 1 Reply Last reply 8 Dec 2020, 02:51
      2
      • F Offline
        F Offline
        fcarney
        wrote on 19 Nov 2020, 20:07 last edited by
        #3

        I cannot remember everything I was doing in this code. But I was trying to do conversions. Here are some of the ways I was testing things. I thought text in QML was UTF-16, but I could be wrong on that:

        // from utf32 to utf16
            // https://unicodebook.readthedocs.io/unicode_encodings.html#surrogates
            function surrogate(input){
                var output
                if(input < 0x10000 || input > 0x10FFFF){
                    return output
                }
        
                var code = input - 0x10000
                var low = 0xD800 | (code >> 10)
                var high = 0xDC00 | (code & 0x3FF)
                output = String.fromCharCode(low) + String.fromCharCode(high)
        
                return output
            }
        
            title: qsTr("Font Testing 💩 😀 %1 %2 %3").arg(surrogate(0x1F4A9)).arg("\u{1F4A9}").arg(String.fromCodePoint(0x1F4A9))
        

        Is text UTF8 or UTF16 in QML?

        C++ is a perfectly valid school of magic.

        C 1 Reply Last reply 8 Dec 2020, 02:57
        1
        • K KroMignon
          19 Nov 2020, 07:17

          @Crawl-W I think you should use the unicode notation \u2103 (works for me ™)

          Text {
              text: "\u2103"
              font.pixelSize: 24
          }
          
          C Offline
          C Offline
          Crawl.W
          wrote on 8 Dec 2020, 02:51 last edited by
          #4

          @KroMignon Yes, it works! I use \u2103 for Celsius, which is utf-16 encoding.

          1 Reply Last reply
          0
          • F fcarney
            19 Nov 2020, 20:07

            I cannot remember everything I was doing in this code. But I was trying to do conversions. Here are some of the ways I was testing things. I thought text in QML was UTF-16, but I could be wrong on that:

            // from utf32 to utf16
                // https://unicodebook.readthedocs.io/unicode_encodings.html#surrogates
                function surrogate(input){
                    var output
                    if(input < 0x10000 || input > 0x10FFFF){
                        return output
                    }
            
                    var code = input - 0x10000
                    var low = 0xD800 | (code >> 10)
                    var high = 0xDC00 | (code & 0x3FF)
                    output = String.fromCharCode(low) + String.fromCharCode(high)
            
                    return output
                }
            
                title: qsTr("Font Testing 💩 😀 %1 %2 %3").arg(surrogate(0x1F4A9)).arg("\u{1F4A9}").arg(String.fromCodePoint(0x1F4A9))
            

            Is text UTF8 or UTF16 in QML?

            C Offline
            C Offline
            Crawl.W
            wrote on 8 Dec 2020, 02:57 last edited by
            #5

            @fcarney Yes, My test and @KroMignon 's demo also tends to uft-16.

            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