前言
当我们要在QTextBrowser内显示内容时总想要以颜色/大小等来区分不同内容,本文展示在文本框内显示字符时自定义颜色和大小
代码
#define TEXT_SIZE_SMALL "1.5"
#define TEXT_SIZE_MEDIUM "1.7"
#define TEXT_SIZE_LARGE "1.9"
#define TEXT_SIZE_OVER "2.5"
#define TEXT_COLOR_RED(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:red;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
#define TEXT_COLOR_BLUE(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:blue;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
#define TEXT_COLOR_GREEN(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:green;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
#define TEXT_COLOR_GREY(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:grey;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
#define TEXT_COLOR_WHILE(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:#FFFFFF;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
#define TEXT_COLOR_YELLOW(STRING, SIZE) \
("<font style = 'font-size:" + QString(SIZE) + "rem; color:#FFFF00;'>" + QString(STRING) \
+ "</font>" \
"<font color=black> </font>")
这段代码是对颜色做了固定,针对不同颜色使用不同宏定义,不过可以进行修改,将颜色也修改为宏定义参数
使用方法:
ui->textBrowser->append(TEXT_COLOR_BLUE("[2024-03-05 10:51:34.871]",TEXT_SIZE_LARGE));
ui->textBrowser->append(TEXT_COLOR_WHILE("[0:01:51]",TEXT_SIZE_MEDIUM));
效果如下: