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. A way to programmatically generate a string literal (is that even the right word)?
QtWS25 Last Chance

A way to programmatically generate a string literal (is that even the right word)?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 343 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I am trying to understand if there is a simpler way of converting from utf32 to utf16 as used in QML strings (I think). I am playing with some code used to calculate the surrogates of emojis:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        id: window
    
        visible: true
        width: 640
        height: 480
    
        // 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
        }
    
        function fromutf32(input){
            return "\\u{%1}".arg(input.toString(16))
        }
    
        title: qsTr("Font Testing 😀 %1 %2 %3").arg(surrogate(0x1F4A9)).arg("\u{1F4A9}").arg(fromutf32(0x1F4A9))
    }
    

    I know I could drop down to c++ for this and just use the functions there. I just find it interesting that all the emoji references provide numbers in utf32 (I think). I wanted a direct way to use these integers in my QML code. That was why I found and used that surrogate function. I also am trying to see if I can create the string that allows me to directly use the code in my code:

    "\u{1F4A9}"
    

    However I cannot figure out how to programmatically convert a string like from my fromutf32 function to the actual string produced if I type it out in a string constant. I tried eval, unescape, and String.raw. I just am not seeing a way to do this. So maybe the surrogate function is the best way to do this. Or like I said before, I should be dropping to c++ for this.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Well I haven't found a way to generate a string literal, but I did find this interesting function of String:

      String.fromCodePoint(0x1F4A9)
      

      It seems to take a utf32 integer and produce the appropriate utf16.

      C++ is a perfectly valid school of magic.

      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