Results 1 to 2 of 2

Thread: indent selection in QPlainTextEdit

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default indent selection in QPlainTextEdit

    I do this to increase the indent of a selection of lines in a plainTextEdit, but nothing happens:
    Qt Code:
    1. QTextCursor cur = plainEdit->textCursor(); // contains the selection
    2. QTextBlockFormat tbf = cur.blockFormat(); //selection format
    3. tbf.setIndent(10); // change indent
    4. cur.setBlockFormat(tbf); // put it all back
    5. plainEdit->setTextCursor(cur);
    To copy to clipboard, switch view to plain text mode 
    wordwrap is off so a block is a line. This doesn't work, somebody has some pointers? thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: indent selection in QPlainTextEdit

    This is my code to do this, I add a space instead of doing a real indent, but it is fine for me. If anyone can make this more efficient, I am all ears!
    Qt Code:
    1. QTextCursor cur = ETEditor->textCursor();
    2. int a = cur.anchor();
    3. int p = cur.position();
    4.  
    5. cur.setPosition(a);
    6. cur.movePosition(QTextCursor::StartOfBlock,QTextCursor::MoveAnchor);
    7. a = cur.position();
    8. // save a new anchor at the beginning of the line of the selected text
    9. cur.setPosition(a);
    10. cur.setPosition(p, QTextCursor::KeepAnchor);
    11. // set a new selection with the new beginning
    12. QString str = cur.selection().toPlainText();
    13. QStringList list = str.split("\n");
    14. // get the selected text and split into lines
    15.  
    16. for (int i = 0; i < list.count(); i++)
    17. list[i].insert(0," ");
    18. //insert a space at the beginning of each line
    19.  
    20. str=list.join("\n");
    21. cur.removeSelectedText();
    22. cur.insertText(str);
    23. // put the new text back
    24. cur.setPosition(a);
    25. cur.setPosition(p, QTextCursor::KeepAnchor);
    26. // reselect the text for more indents
    27.  
    28. ETEditor->setTextCursor(cur);
    29. // put the whole thing back into the main text
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. trying to indent
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 19th July 2010, 23:25
  2. QPlainTextEdit selection color?
    By jajdoo in forum Newbie
    Replies: 3
    Last Post: 18th July 2010, 09:39
  3. Qt Creator How can I modify auto-indent format ?
    By cionci in forum Qt Tools
    Replies: 1
    Last Post: 4th March 2010, 09:49
  4. QTreeView indent
    By evgenM in forum Newbie
    Replies: 0
    Last Post: 18th August 2009, 11:53
  5. QPlainTextEdit
    By Carlsberg in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2009, 06:12

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.