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.9k 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 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?

    kshegunovK 1 Reply Last reply
    0
    • H huse

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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 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?

        kshegunovK 1 Reply Last reply
        0
        • H huse

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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 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?

            kshegunovK 1 Reply Last reply
            0
            • H huse

              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?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on 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

              • Login

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