Showing posts with label wordwrap. Show all posts
Showing posts with label wordwrap. Show all posts

Wednesday, March 4, 2009

Word Wrap Code

This is a code to wrap a QString if it exceeds a particular number of characters per line, without splitting any word into different lines.

Signature : QString wordWrap(const QString &str, int wrapLength)
Argument : 1. The QString to be wrapped
2. The permissible number of characters per line.

Return Value: The wrapped QString

QString wordWrap(const QString &str, int wrapLength)
{
QString tempStr= str;
int len = str.length(), pos= (wrapLength>1)?wrapLength -1:1;

while(pos < len-1){

int tempPos=pos;
while(tempStr.at(tempPos)!=' ' && tempPos > 0){

tempPos--;
}
if(tempPos > 0)pos=tempPos;

tempStr.replace(pos,1,'\n');
pos+=pos;
}

return tempStr;
}