Results 1 to 8 of 8

Thread: Text Manipulation.

  1. #1
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Question Text Manipulation.

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Text Manipulation.

    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.

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text Manipulation.

    you can use QString::mid to extract a part of a string

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text Manipulation.

    Something like this should work:
    Qt Code:
    1. QString myString = ...;
    2. QList<int> array = ...;
    3. int pos = 0;
    4. foreach(int arrPos, array){
    5. myString.insert(pos+arrPos, '\n');
    6. pos+=arrPos+1;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default Re: Text Manipulation.

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text Manipulation.

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default Re: Text Manipulation.

    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 ?

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Text Manipulation.


Similar Threads

  1. Sending over UDP - Binary Manipulation
    By Cotlone in forum Newbie
    Replies: 6
    Last Post: 9th June 2010, 09:24
  2. Replies: 4
    Last Post: 8th May 2010, 11:40
  3. Background manipulation of QPlainTextEdit
    By djjkotze in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 13:34
  4. String manipulation methods?
    By PaladinKnight in forum Newbie
    Replies: 5
    Last Post: 16th March 2010, 23:44
  5. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.