[solved] Getting the text lines of a QML Text object
-
wrote on 20 Nov 2014, 08:23 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!
-
Hi,
Can you elaborate what do you mean by text lines and array of text lines ?
-
wrote on 20 Nov 2014, 09:57 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 nuncThe 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.
-
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. -
wrote on 20 Nov 2014, 10:59 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? -
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.
-
wrote on 20 Nov 2014, 11:39 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 INIf 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.
-
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.
-
wrote on 20 Nov 2014, 12:46 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 :) -
wrote on 21 Nov 2014, 04:26 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
-
wrote on 26 Nov 2014, 11:11 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/11