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. How to correctly append string to string with surrogate pairs (utf16)?
Forum Updated to NodeBB v4.3 + New Features

How to correctly append string to string with surrogate pairs (utf16)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.8k Views 1 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.
  • H Offline
    H Offline
    huse
    wrote on 22 Sept 2017, 23:18 last edited by
    #1

    I have a QString which includes a surrogate pair and I want to append some other string to it.

    QString convertUnicodeToString(uint32_t symbol)
    {
    QString str;
    QChar charArray[2];
    charArray[0] = QChar::highSurrogate(symbol);
    charArray[1] = QChar::lowSurrogate(symbol);
    str = QString(charArray, 2);
    return str;
    }

    QString a = "a";
    QString b = convertUnicodeToString(QChar(0x1d160));

    Then I use a painter to draw the text in a qwidget.
    Method 1: a.append(b) works fine. I get the letter 'a' followed by a music symbol
    Method 2: b.append(a) doesn't work. I get the two symbols on the same place.

    So at the moment, to get them next to each other, I'll have to do this:
    Method 3: b.append(" ").append(a) (two blank spaces is needed!)

    Do I need to insert '\0', BOM or some other thing to mark the end of the surrogate-pair-part of the string?
    There must be a better way to do this than method 3?

    K 1 Reply Last reply 23 Sept 2017, 13:12
    0
    • H huse
      22 Sept 2017, 23:18

      I have a QString which includes a surrogate pair and I want to append some other string to it.

      QString convertUnicodeToString(uint32_t symbol)
      {
      QString str;
      QChar charArray[2];
      charArray[0] = QChar::highSurrogate(symbol);
      charArray[1] = QChar::lowSurrogate(symbol);
      str = QString(charArray, 2);
      return str;
      }

      QString a = "a";
      QString b = convertUnicodeToString(QChar(0x1d160));

      Then I use a painter to draw the text in a qwidget.
      Method 1: a.append(b) works fine. I get the letter 'a' followed by a music symbol
      Method 2: b.append(a) doesn't work. I get the two symbols on the same place.

      So at the moment, to get them next to each other, I'll have to do this:
      Method 3: b.append(" ").append(a) (two blank spaces is needed!)

      Do I need to insert '\0', BOM or some other thing to mark the end of the surrogate-pair-part of the string?
      There must be a better way to do this than method 3?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 23 Sept 2017, 13:12 last edited by
      #2
      uint32_t symbol;
      
      QString a = "a";
      a += QChar(symbol);
      

      ?
      QString already keeps the data internally as unicode.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • H Offline
        H Offline
        huse
        wrote on 23 Sept 2017, 14:06 last edited by
        #3

        Thanks for your reply, but I don't get this to work for:
        uint32_t symbol = 0x1D161;
        It shows a + some chinese character?

        K 1 Reply Last reply 23 Sept 2017, 20:27
        0
        • H huse
          23 Sept 2017, 14:06

          Thanks for your reply, but I don't get this to work for:
          uint32_t symbol = 0x1D161;
          It shows a + some chinese character?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 23 Sept 2017, 20:27 last edited by
          #4

          Well, this is larger than a single short, so you'd need to use QString::fromUtf16 if you need decoding the utf16.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • H Offline
            H Offline
            huse
            wrote on 23 Sept 2017, 22:40 last edited by
            #5

            Ok, maybe there is something I don't understand here:) or maybe the problem is related to the font or painter.

            When I try this:
            QString test = "a";
            test+=QString::fromUtf16(u"\U0001D161");
            test+="a";
            painter.drawText(500,500,test);
            0_1506205443773_screenshot_qt.png

            So, I get the correct symbols (a + musical note +a), but the last two characters (musical note+a) are still drawn at the same place like in method 2 above.

            Also, when I measure the width:
            QFontMetrics f = painter.fontMetrics();
            f.width(QString::fromUtf16(u"\U0001D161")); //returns 0
            f.boundingRect(QString::fromUtf16(u"\U0001D161")).width(); //returns 19

            Is it possible that QPainter::drawText() isn't moving when drawing the next character, because the width is 0?

            K 1 Reply Last reply 26 Sept 2017, 21:50
            0
            • H huse
              23 Sept 2017, 22:40

              Ok, maybe there is something I don't understand here:) or maybe the problem is related to the font or painter.

              When I try this:
              QString test = "a";
              test+=QString::fromUtf16(u"\U0001D161");
              test+="a";
              painter.drawText(500,500,test);
              0_1506205443773_screenshot_qt.png

              So, I get the correct symbols (a + musical note +a), but the last two characters (musical note+a) are still drawn at the same place like in method 2 above.

              Also, when I measure the width:
              QFontMetrics f = painter.fontMetrics();
              f.width(QString::fromUtf16(u"\U0001D161")); //returns 0
              f.boundingRect(QString::fromUtf16(u"\U0001D161")).width(); //returns 19

              Is it possible that QPainter::drawText() isn't moving when drawing the next character, because the width is 0?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 26 Sept 2017, 21:50 last edited by
              #6

              @huse said in How to correctly append string to string with surrogate pairs (utf16)?:

              Is it possible that QPainter::drawText() isn't moving when drawing the next character, because the width is 0?

              Yes, but I really don't know why that is. Perhaps it's some problem with the font metrics, i.e. your font doesn't support (well) that particular character, but to be completely honest I'm just speculating.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1

              2/6

              23 Sept 2017, 13:12

              4 unread
              • Login

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