Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [solved] Getting the text lines of a QML Text object

    QML and Qt Quick
    3
    11
    2275
    Loading More Posts
    • 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.
    • K
      Katlin last edited by

      [in QT 5.2.1]

      Hi,

      I need help with the following problem: to get the actual text lines of a Text object, inside QML (not-- C++).
      The text object has a fixed width and height, and also it has a specified font (font family, size, weight,italics,line height, -etc) and the Word Wrap is enabled (the text can be bigger than the display area).

      So for my given Text object and text, I need an array with the text lines.

      Any input is helpfull.

      Thx!

      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        Hi,

        Can you elaborate what do you mean by text lines and array of text lines ?

        157

        1 Reply Last reply Reply Quote 0
        • K
          Katlin last edited by

          Example:

          The text is:
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pulvinar mollis nibh non convallis. Nunc sed fringilla augue, sit amet varius nunc

          The Text object size is:
          [------------------------]

          What I need:
          Array(
          'Lorem ipsum dolor sit amet,',
          'consectetur adipiscing elit.',
          'Vivamus pulvinar mollis',
          'nibh non convallis. Nunc',
          'sed fringilla augue,',
          'sit amet varius nunc'
          )

          (can be json or whatever, as long as I get the actual text lies)

          Where the actual lines are given by the Text object width and the fact that WordWrap is on.

          1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators last edited by

            Sorry but i still don't understand. Is wordwrap not working ?
            Or you want to get the Text data in QML as it is displayed in Text element ? If so then you will need to split it manually.

            157

            1 Reply Last reply Reply Quote 0
            • K
              Katlin last edited by

              Yes, I just want the text data as it is displayed in the Text element, for the specified width and font metrics.
              Assuming I have to do this manually, what do you suggest? How should I approach this?

              1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators last edited by

                Why not do it in the reverse way ? ie. Creating a string with "\n" inserted in between the string "Lorem ipsum dolor ......" after every n number of words and then setting this string to Text element.

                157

                1 Reply Last reply Reply Quote 0
                • K
                  Katlin last edited by

                  Because that is not the point.
                  What you are suggesting is a different problem, it is in reverse, as you say :)
                  I do not want to split the text myself, I want it splitted according to the Text object.

                  Again:

                  IN : Text object width, font metrics , the whole text (string)
                  OUT: array of strings - the whole text splitted according to the IN

                  If QML would allow, I would just do:
                  @
                  Text{
                  id:my_text
                  text : "Lorem ipsum dolor sit amet ...."
                  wrapMode: Text.WordWrap;
                  font.family: ...
                  font.bold: ...
                  font.italic: ...
                  font.pointSize: ...

                  Component.onCompleted: {
                  
                      var a = new Array();
                      a = my_text.lines(); // or something like that
                  
                  }
                  

                  }
                  @

                  But QML doesn't permit, hence my question.

                  1 Reply Last reply Reply Quote 0
                  • p3c0
                    p3c0 Moderators last edited by

                    AFAIK there's no built-in way to do that. The text is finally rendered by considering all it's properties. You will need to find the way how Qt does it so that you can emulate it on QML side.

                    157

                    1 Reply Last reply Reply Quote 0
                    • K
                      Katlin last edited by

                      Yes, i was thinking of that, but I wanted to check with the community before coding anything.
                      So in other words, the best and only way to do it is to do it by doing it :)

                      1 Reply Last reply Reply Quote 0
                      • C
                        chrisadams last edited by

                        Hi,

                        See QRectF QQuickTextPrivate::setupTextLayout(qreal *const baseline) for the related code. It uses QTextLayout and QTextLine internally, along with some geometry constraints implied by the geometry of the QQuickText instance properties.

                        Beware, though: text layout code often involves dragons.

                        Kind regards,
                        Chris Adams.

                        http://www.qinetic.com.au/ - Qt & QML User Experience Specialists

                        1 Reply Last reply Reply Quote 0
                        • K
                          Katlin last edited by

                          Solved! - in case anybody else needs something like this - here's my solution:

                          @
                          QStringList ret;
                          QString temp;
                          QTextLayout blob;
                          QTextOption to;
                          to.setWrapMode(QTextOption::WordWrap);
                          to.setUseDesignMetrics(true);
                          QFont ft = font();
                          ft.setPixelSize(...);
                          blob.setFont(...);
                          blob.setText(...);
                          blob.setTextOption(to);
                          blob.beginLayout();
                          Q_FOREVER
                          {
                          QTextLine line = blob.createLine();

                              if(!line.isValid()){
                                  break;
                              }
                              line.setLineWidth(width());
                          
                              temp = the_text.mid(line.textStart(),line.textLength());
                              ret << temp;
                          }
                          return ret;
                          

                          @

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post