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. [solved] Getting the text lines of a QML Text object
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 3 Posters 3.0k 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.
  • K Offline
    K Offline
    Katlin
    wrote on last edited by
    #1

    [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
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

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

      157

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Katlin
        wrote on last edited by
        #3

        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
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          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
          0
          • K Offline
            K Offline
            Katlin
            wrote on last edited by
            #5

            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
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              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
              0
              • K Offline
                K Offline
                Katlin
                wrote on last edited by
                #7

                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
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  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
                  0
                  • K Offline
                    K Offline
                    Katlin
                    wrote on last edited by
                    #9

                    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
                    0
                    • C Offline
                      C Offline
                      chrisadams
                      wrote on last edited by
                      #10

                      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
                      0
                      • K Offline
                        K Offline
                        Katlin
                        wrote on last edited by
                        #11

                        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
                        0

                        • Login

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