View Full Version : Text Manipulation.

22nd July 2010, 08:24

I have a long string, and I want to wrap this string.
Also, I have an array which contains the width for each wrapped substring.
Number of elements in the array, are the number of lines across the string to wrapped.
Each element in the array represents the width of the line.


string :"Using Qt, you can write web-enabled applications once and deploy them across desktop, mobile and embedded operating systems without rewriting the source code."

array : {50, 50, 40, 10}

Now I should be able to extarct the substring from the above string as below.

"Using Qt, you can write web-enabled applications o"
"nce and deploy them across desktop, mobile and emb"
"edded operating systems without rewritin"
"g the s..."

Is there any method or class in Qt which will help manipulating the text as mentioned above?


22nd July 2010, 08:29
Sure QTextStream::read(length)

Create a text stream based on the string of your complete text. Then loop through the array of substring lengths. Use the stream read function to read the substring of the correct length.

22nd July 2010, 09:04
you can use QString::mid to extract a part of a string

22nd July 2010, 10:10
Something like this should work:

QString myString = ...;
QList<int> array = ...;
int pos = 0;
foreach(int arrPos, array){
myString.insert(pos+arrPos, '\n');

26th July 2010, 08:50

Further to this, I tried to do

QTextEdit textEdit(str);

So, if the above code performs proper wrapping of 'str', then is it possible to extract line by line from 'textEdit' ?

26th July 2010, 09:47
No, I don't think so but why would you want to do that? It won't wrap according to your array. If you want static "wrapping", use QFontMetrics to determine where each line should be terminated or use QTextLayout.

26th July 2010, 10:05
Thanks wysota.

Actually my problem is,
I have a long string and a rectangular region. Now I want to fit the string into that rectangular box.
After breaking down the long string depending on the size of the box and wrapping it, I want to extract the individual lines from the box, and process further.
So, in Qt is there a way to do it ?

26th July 2010, 11:07
You could have a look at http://doc.trolltech.com/qq/qq24-textlayouts.html.