PDA

View Full Version : Text Manipulation.



suneel1310
22nd July 2010, 07:24
Hi,

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.

Example:

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?

Thanks

tbscope
22nd July 2010, 07:29
Sure QTextStream::read(length)
http://doc.qt.nokia.com/4.6/qtextstream.html#read

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.

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

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

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

suneel1310
26th July 2010, 07:50
Thanks,

Further to this, I tried to do

QTextEdit textEdit(str);
textEdit.setLineWrapColumnOrWidth(width);
textEdit.setLineWrapMode(QTextEdit::WidgetWidth);

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

wysota
26th July 2010, 08: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.

suneel1310
26th July 2010, 09: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 ?

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