Results 1 to 3 of 3

Thread: How to change the text color in QPlainTextEditor?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Exclamation How to change the text color in QPlainTextEditor?

    Qt Code:
    1. void writeToEditor( QString partOfText, double readBytes )
    2. {
    3. QString newFormat;
    4. for( int i = 0; i < readBytes; i = i + 10 )
    5. {
    6. QStringRef subString( &partOfText, i, 10 );
    7. newFormat.append( subString );
    8. newFormat.append( " " );
    9. }
    10.  
    11. objQPlainTextEdit.appendPlainText( newFormat );
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Here objQPlainTextEdit is an object of QPlainTextEdit.

    Considering: https://doc.qt.io/qt-5/qplaintextedit.html

    Each character within a paragraph has its own attributes, for example, font and color.
    I can see there is QPalette. How to use it to color the alphabets displayed in QPlainTextEdit, differently?

    For example: 'A' should be of red color, and 'B' should be of green colour.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change the text color in QPlainTextEditor?


  3. The following user says thank you to ChristianEhrlicher for this useful post:

    TheIndependentAquarius (5th September 2020)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to change the text color in QPlainTextEditor?

    I solved it.

    Here is the working code:
    Qt Code:
    1. QString combineHtml = "<p>";
    2.  
    3. for( int j = 0; j < readBytes; j++)
    4. {
    5. if( partOfText[j] == 'A' )
    6. {
    7. combineHtml.append( QString( "<span style = 'color: red'>")
    8. + QString( partOfText[j]) + QString( "</span>"));
    9. }
    10. else
    11. {
    12. combineHtml.append( QString( "<span style = 'color: blue'>")
    13. + QString( partOfText[j]) + QString( "</span>"));
    14. }
    15. }
    16.  
    17. combineHtml.append( "</p>");
    18.  
    19. std::cout << combineHtml.toStdString();
    20.  
    21. objQPlainTextEdit.appendHtml( combineHtml );
    To copy to clipboard, switch view to plain text mode 

    Here partOfText is QString, and objQPlainTextEdit is an object of QPlainTextEdit.

    Output generated by the above code is as follows:

    Qt Code:
    1. <p><span style = 'color: blue'>C</span><span style = 'color: red'>A</span><span style = 'color: red'>A</span><span style = 'color: blue'>T</span><span style = 'color: blue'>C</span><span style = 'color: blue'>T</span><span style = 'color: blue'>C</span><span style = 'color: blue'>C</span><span style = 'color: red'>A</span><span style = 'color: blue'>T</span></p>]
    To copy to clipboard, switch view to plain text mode 

    11.png

Similar Threads

  1. How to draw a rectangle around selected text in QPlainTextEditor in Qt?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2020, 14:01
  2. Replies: 4
    Last Post: 19th July 2016, 20:10
  3. change text color on QRadioButton
    By vonCZ in forum Newbie
    Replies: 4
    Last Post: 7th November 2012, 21:05
  4. Replies: 3
    Last Post: 16th July 2012, 16:04
  5. Replies: 3
    Last Post: 22nd January 2010, 17:46

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.