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. Issue with using text wrap for international language
QtWS25 Last Chance

Issue with using text wrap for international language

Scheduled Pinned Locked Moved QML and Qt Quick
18 Posts 6 Posters 11.8k 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.
  • T Offline
    T Offline
    tuanster
    wrote on last edited by
    #1

    I am having an issue with wrapping text within a QML Text element for foreign languages (language that uses Unicode characters like Korean or Chinese). I have the width defined and wrapMode set to Text.WordWrap for my Text element. It works for English language, but when I try to use it for another language, it doesn't work. The text doesn't properly wrap around a blank space as it should.

    Any tip is truly appreciated.

    Thanks!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      Would you be able to paste an example showing this issue?

      Thanks,
      Michael

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tuanster
        wrote on last edited by
        #3

        Sure, here is a simple example to illustrate the issue.

        @import QtQuick 1.0

        Rectangle {
        width: 360
        height: 360

        Text {
            id: textDisplay
            anchors.centerIn: parent
            textFormat: Text.StyledText
            text: "해리 포터와 죽음의 성물-1부"
            width: 100
            font.wordSpacing: 0
            wrapMode: Text.WordWrap
            horizontalAlignment: Text.AlignLeft
        }
        

        }@


        Output:

        해리 포터와 죽
        음의 성물-1부

        As you can see, the issue is that on the first line of the output, the line is being wrapped in the middle of the third word.

        It works fine for English text or (text with Ascii characters). This issue is observed in language that is non-Ascii.

        Please help.

        Thanks!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Hi,

          Chinese (and as I understand it Korean as well [edit: on further research, this is incorrect for Korean]) are typically written without explicit whitespace between "words" -- this may be why WordWrap allows wrapping at any character boundary. Do you have any experience with other toolkits and how they handle this? (this is Qt-wide behavior, and not specific to QML). Can you give further detail/links on how this mode of wrapping is used for these languages (maybe Qt needs a WrapAtWhitespace option?)?

          Regards,
          Michael

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tuanster
            wrote on last edited by
            #5

            Michael, thanks for the reply. I thought Qt uses whitespace to detect the beginning/ending of a word and that's how it's supposed to work for WordWrap. What do you recommend for solving this issue at the QML level?

            I wrote a function in C++ plugin that would take in the font pixel size and the field width and based on that it would break the string into separate lines by adding a QChar::LineSeparator to the original string.

            It kind of works but it's not optimal because it doesn't take into account other things such as text font, italics, word space, boldness and other things.

            What solution would you recommend?

            Thanks much.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              Hi,

              I'm not sure what to suggest -- from what I understand the WordWrap behavior of Qt/QML is correct for these languages (they rightly break at any character boundary rather than just on explicit whitespace). If you could explain further why you need breaks only on whitespace (or could point to other toolkits/articles/etc regarding this) that would be very helpful.

              Regards,
              Michael

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tuanster
                wrote on last edited by
                #7

                Hi Michael,

                Here is the problem that we're having. We have an application that displays the title of a movie in a Text element with width of 100.

                Let's take the movie, "Harry Potter And The Deathly." In English, this text is being wrapped correct at the word boundary.

                In Korean, the text is "해리 포터와 죽음의 성물-1부" and as you notice, there's a space in between words. When you run the code I posted, you will see that the line breaks at the end of the first character of the third word. It doesn't look good on my application.

                If Korean language is treated the same way as English by Qt, I would think it should break into a new line at the end of a word.

                Thanks!

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mbrasser
                  wrote on last edited by
                  #8

                  Hi,

                  Okay, thanks for the additional information. I'm not familiar with Korean, and mistakenly thought it was similar to Chinese in the use of whitespace.

                  Articles like http://msdn.microsoft.com/en-us/goglobal/bb688158.aspx#EEE seem to suggest that breaking between two characters is a more common case for Korean (which is different than English). Experimenting with e.g. TextEdit on OS X and IE on Windows seem to show similar default line-breaking for Korean (usually breaks on character rather than whitespace), while Office seems to break on whitespace by default.

                  If you can give examples or links showing that wrap-at-whitespace is a common/useful option for Korean (the above link at least suggests that it's an option in some programs), I'd highly suggest raising a bug for it using the "bug tracker":http://bugreports.qt.nokia.com.

                  Unfortunately I can't think of any great solutions for getting WrapAtWhitespaceOnly-type behavior for Korean from Qt (though you could try experimenting with using the lower-level text APIs in Qt like QTextLayout, etc).

                  Regards,
                  Michael

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    As a work-around, would it be acceptable to use rich text, and put the different words in <span> elements for the time being? That should force wrapping at the spaces.

                    "해리 포터와 죽음의 성물-1부" would become
                    @
                    "<html><span>해리</span> <span>포터와</span> <span>죽음의</span> <span>성물-1부</span></html>"
                    @

                    For inserting this for single sentences (no line breaks), something like this might work:
                    @
                    #include <QStringBuilder>

                    QString spanify(const QString& input)
                    {
                    QStringList elements = input.split(QRegExp("\s"));
                    QString result = QLatin1String("<html>");
                    foreach (const QString element, elements) {
                    result = result % QLatin1String("<span>") % element % QLatin1String("</span> ");
                    }
                    result.chop(1); //remove the last space;
                    result = result % QLatin1String("</html>");

                    return result;
                    

                    }
                    @

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tuanster
                      wrote on last edited by
                      #10

                      Andre,

                      I just tried your solution and it still doesn't work. I tried putting the "<html><span>해리</span> <span>포터와</span> <span>죽음의</span> <span>성물-1부</span></html>" as the text value and changing the text format to rich text, but it's still showing the same output as before. In other words, it doesn't force wrapping at the spaces.

                      Do you have other ideas for a work-around?

                      Thanks!

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        [quote author="tuanster" date="1302192466"]Andre,

                        I just tried your solution and it still doesn't work. I tried putting the "<html><span>해리</span> <span>포터와</span> <span>죽음의</span> <span>성물-1부</span></html>" as the text value and changing the text format to rich text, but it's still showing the same output as before. In other words, it doesn't force wrapping at the spaces.

                        Do you have other ideas for a work-around?

                        Thanks!

                        [/quote]

                        Sorry, I'm out of ideas. It seemed that that should work, but I guess I was wrong. Sorry to have led you down a blind alley.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Just a guess: try to replace

                          @
                          <span>xxx</span>
                          // with
                          <span style="white-space:nowrap">xxx</span>
                          @

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tuanster
                            wrote on last edited by
                            #13

                            Thanks for another suggestion.

                            I just tried it with the "white-space: nowrap" but no luck. It still doesn't work. If I put other attribute like color inside the span tag, the text's color is changed. But the text is still being wrapped in the middle of a word.

                            QML Text element has a mind of its own.

                            Any other ideas?

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              loma27
                              wrote on last edited by
                              #14

                              Hello,

                              Please try this.

                              I have used Webview. I have tried this and it seems to be working fine for me.
                              If this is feasible(changing from Text to Webview) or affordable for your application. This may work for you.

                              @import QtQuick 1.0
                              import QtWebKit 1.0

                              Rectangle {
                              width: 360
                              height: 360

                              WebView{
                                  html : "<div>해리 포터와 죽음의 성물부</div>"
                              }
                              

                              }@

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                loma27
                                wrote on last edited by
                                #15

                                sorry for wrong code. Please check updated.

                                @WebView{
                                html : "<div>해리 포터와 죽음의 성물부</div>"
                                }
                                }@

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  loma27
                                  wrote on last edited by
                                  #16

                                  i have given style width 100px to div but its not coming in comments.

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    tuanster
                                    wrote on last edited by
                                    #17

                                    Hi Ioma27,

                                    Thanks for your recommendation. It turns out you also have to add the span tag for it to work.

                                    A sample working code is this:

                                    WebView {
                                        html : "<div><span>해리</span> <span>포터와</span> <span>죽음의</span> <span>성물부</span></div>"
                                        preferredWidth: 140
                                        preferredHeight: 400
                                    }
                                    
                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Minkyu Park
                                      wrote on last edited by
                                      #18

                                      Seems there is a patch applied to 5.14
                                      https://code.qt.io/cgit/qt/qtbase.git/commit/?id=d1bab5b1e3d28c0e2925ad3208cea785af755d54

                                      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