How to change the text color in QPlainTextEditor?
Code:
void writeToEditor
( QString partOfText,
double readBytes
) {
for( int i = 0; i < readBytes; i = i + 10 )
{
QStringRef subString( &partOfText, i, 10 );
newFormat.append( subString );
newFormat.append( " " );
}
objQPlainTextEdit.appendPlainText( newFormat );
}
Here objQPlainTextEdit is an object of QPlainTextEdit.
Considering: https://doc.qt.io/qt-5/qplaintextedit.html
Quote:
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.
Re: How to change the text color in QPlainTextEditor?
1 Attachment(s)
Re: How to change the text color in QPlainTextEditor?
I solved it.
Here is the working code:
Code:
for( int j = 0; j < readBytes; j++)
{
if( partOfText[j] == 'A' )
{
combineHtml.
append( QString( "<span style = 'color: red'>") }
else
{
combineHtml.
append( QString( "<span style = 'color: blue'>") }
}
combineHtml.append( "</p>");
std::cout << combineHtml.toStdString();
objQPlainTextEdit.appendHtml( combineHtml );
Here partOfText is QString, and objQPlainTextEdit is an object of QPlainTextEdit.
Output generated by the above code is as follows:
Code:
<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>]
Attachment 13536