Home > Mobile >  Qt5 QTextEdit how to set the paragraph different background color
Qt5 QTextEdit how to set the paragraph different background color

Time:09-24

Background: there are Q3TextEdit qt3: : setParagraphBackgroundColor, but qt5 canceled,

Increasing demand: qtextedit in append mode paragraphs of text, to set the background color, the paragraph but how to set up the paragraph different background color?

Try:
No effect in setTextBackgroundColor
2. Use setStyleSheet (" QLineEdit {background - color: black; } "); Whole set the background color, the only way not segmented
3. Use QPalette: : Base again setPalette also only whole set the background color, can't block

CodePudding user response:

You can use QTextBrowser QTextEdit instead, QTextBrowser can set HTML text style, more in line with your requirements,

CodePudding user response:

Can try listwidget or listview, each line color can be set directly

CodePudding user response:

Set the HTML, through HTML to control the color

CodePudding user response:

Can also use the inserted way insertHtml

CodePudding user response:



Above is a piece of HTML content, and the other one is effect, according to the color of the two HTML (light blue and red) should be different, but the display is light blue, what is the problem?

CodePudding user response:

To learn

CodePudding user response:

One way is to modify the current font background, eg:
 
QTextEdit * pTextEdit=new QTextEdit;
for (int i=0; I & lt; 10; I++) {
QBrush brush (QColor (255, 0, 0));
QTextCharFormat format;
Format. SetBackground (brush);
PTextEdit - & gt; SetCurrentCharFormat (format);
PTextEdit - & gt; Append (" * * * * * * ");
}

Another way is to modify the paragraph has been added, you can refer to:
https://www.cnblogs.com/lehoho/p/11453471.html

CodePudding user response:

reference 7 floor of Yi reply:
is a way to modify the current font background, eg:
 
QTextEdit * pTextEdit=new QTextEdit;
for (int i=0; I & lt; 10; I++) {
QBrush brush (QColor (255, 0, 0));
QTextCharFormat format;
Format. SetBackground (brush);
PTextEdit - & gt; SetCurrentCharFormat (format);
PTextEdit - & gt; Append (" * * * * * * ");
}

Another way is to modify the paragraph has been added, you can refer to:
https://www.cnblogs.com/lehoho/p/11453471.html


If it is a plain text using setCurrentCharFormat is useful, but I append is HTML text inside,

CodePudding user response:


reference 1/f, south of the five response:
you can use instead of QTextEdit QTextBrowser QTextBrowser can set the HTML text style, more in line with the requirements of you,


With HTML, but the display is a color

CodePudding user response:

refer to the eighth floor kalyan response:
Quote: refer to 7th floor not Yi response:

One way is to modify the current font background, eg:
 
QTextEdit * pTextEdit=new QTextEdit;
for (int i=0; I & lt; 10; I++) {
QBrush brush (QColor (255, 0, 0));
QTextCharFormat format;
Format. SetBackground (brush);
PTextEdit - & gt; SetCurrentCharFormat (format);
PTextEdit - & gt; Append (" * * * * * * ");
}

Another way is to modify the paragraph has been added, you can refer to:
https://www.cnblogs.com/lehoho/p/11453471.html


If it is a plain text using setCurrentCharFormat is useful, but I append is HTML text inside,


Yes, the two way is only suitable for plain text, apply to your original idea
  •  Tags:  
  • Qt
  • Related