Important: Please read the Qt Code of Conduct -

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

  • [in QT 5.2.1]


    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.


  • Moderators


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

  • 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:
    '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.

  • Moderators

    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.

  • 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?

  • Moderators

    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.

  • 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.


    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 : "Lorem ipsum dolor sit amet ...."
    wrapMode: Text.WordWrap; ...
    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.

  • Moderators

    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.

  • 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 :)

  • 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. - Qt & QML User Experience Specialists

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

    QStringList ret;
    QString temp;
    QTextLayout blob;
    QTextOption to;
    QFont ft = font();
    QTextLine line = blob.createLine();

        temp = the_text.mid(line.textStart(),line.textLength());
        ret << temp;
    return ret;


Log in to reply