【Qt】QPlainTextEdit 设置颜色、设置单行字体颜色、设置背景颜色、设置字体大小

素颜马尾好姑娘i 2023-10-01 09:06 295阅读 0赞

自定义函数

我们需要的操作比较多,可以放入函数内运行
用自定义函数代替plainTextEdit.appendPlainText();
来做到控制每一行字体颜色的效果
/*
输入:文本信息、PlainTextEdit编辑框指针、字体大小、字体颜色、字体背景颜色
*/

  1. void InsertText(QString text, QPlainTextEdit* plainTextEdit,int fontSize, QColor fontColor, QColor backColor)
  2. {
  3. QTextCharFormat fmt;
  4. //字体色
  5. fmt.setForeground(QBrush(fontColor));
  6. //fmt.setUnderlineColor("red");
  7. //背景色
  8. fmt.setBackground(QBrush(backColor));
  9. //字体大小
  10. fmt.setFontPointSize(fontSize);
  11. //文本框使用以上设定
  12. plainTextEdit->mergeCurrentCharFormat(fmt);
  13. //文本框添加文本
  14. plainTextEdit->appendPlainText(text);
  15. }

调用测试

  1. InsertColorText("test black ",22, QColor("black"), ui.OneScanRunInfo);
  2. InsertColorText("test red",22, QColor("red"), ui.OneScanRunInfo);
  3. InsertColorText("test yellow with green back gound", 22,QColor("yellow"), ui.OneScanRunInfo, QColor("green"));

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读

    相关 状态栏字体颜色设置

    根据不同的主题设置状态栏的字体颜色,主要有以下几种实现方法。 > 注意:状态栏的字体颜色分两种,一种是黑色,另一种是白色。 步骤1 设置`info.plist`中的属性