Results 1 to 5 of 5

Thread: How to set TextFormat of a TextBlock directly

  1. #1
    Join Date
    Jan 2012
    Location
    ShangHai, PRC of China
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to set TextFormat of a TextBlock directly

    textDoc = QTextEdit->document;
    ...

    I want to set one line's color, but no effect. I think for the TextBlock of plaintext, block and line are same things.

    Qt Code:
    1. QTextBlock blk = textDoc->begin();
    2. int index = 0;
    3. while(blk.isValid()){
    4. qDebug() << "BLOCK:" << blk.text();
    5. index++;
    6. if(1){//index == 10){
    7. QTextCursor cursor(blk);
    8. format.setForeground(Qt::darkYellow);
    9. format.setBackground(Qt::gray);
    10. cursor.setBlockCharFormat(format);
    11. }
    12. blk = blk.next();
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set TextFormat of a TextBlock directly

    Do you need to have a selection (i.e. selectionStart() then setPosition() or movePosition()) to apply a format to?

  3. #3
    Join Date
    Jan 2012
    Location
    ShangHai, PRC of China
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set TextFormat of a TextBlock directly

    No selection.
    I have a string list, if the lines in QTextEdit same as string in the string list, I'll highlight it. That's my purpose.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to set TextFormat of a TextBlock directly

    The block's character format is used when inserting text into an empty block, something you are not doing. You need to make a selection and then either QTextCursor::setCharFormat() or QTextCursor::mergeCharFormat().

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. te.setText("Line 1\nLine 2\nLine 3");
    9.  
    10. QTextDocument *textDoc = te.document();
    11. QTextBlock blk = textDoc->begin();
    12. int index = 0;
    13. while (blk.isValid()) {
    14. qDebug() << blk.text();
    15. if (index == 1) {
    16. QTextCursor cursor(blk);
    17. cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    18. QTextCharFormat format;
    19. format.setForeground(Qt::darkYellow);
    20. format.setBackground(Qt::gray);
    21. cursor.setCharFormat(format);
    22. }
    23. ++index;
    24. blk = blk.next();
    25. }
    26.  
    27. te.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2012
    Location
    ShangHai, PRC of China
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to set TextFormat of a TextBlock directly

    You are right, if make a selection, it works.
    I tried your code. For plaintext, it should be
    Qt Code:
    1. cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
    To copy to clipboard, switch view to plain text mode 

    Thank you again.

Similar Threads

  1. a Readonly Highlighted TextBlock in QTextEdit?
    By vertusd in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2010, 15:50
  2. Paint to pixmap directly
    By pospiech in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2010, 08:34
  3. setVisible textblock in QTextEdit
    By corrado in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2010, 03:04
  4. Set QHash and QMap value directly
    By wirasto in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2009, 19:21
  5. How do I paint directly ina QGraphcicsView?
    By extrakun in forum Qt Programming
    Replies: 3
    Last Post: 13th July 2009, 09:12

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.