一.主要功能对比
二.关键功能差异
1.文本类型支持
QPlainTextEdit:
仅支持纯文本(Plain Text),不处理任何格式(如字体、颜色、链接、图片等)。文本以原始字符形式存储,适合处理日志、代码、配置文件等无需格式的内容。
例:输入 <font color="red">文本</font> 会被当作普通字符串显示,不会渲染为红色文本。
QTextEdit 与 QTextBrowser:
均支持富文本(Rich Text),可解析和渲染 HTML 格式的文本(如 <b>加粗</b>、<img src="icon.png">),支持字体样式、颜色、段落对齐、表格、图片嵌入等格式。
例:输入 <font color="red">文本</font> 会直接显示为红色文本。
2.编辑能力
QPlainTextEdit 与 QTextEdit:
默认可编辑(用户可输入、删除、修改文本),可通过 setReadOnly(true) 设置为只读模式。
QPlainTextEdit 提供基础编辑功能(换行、撤销 / 重做、选中复制等)。
QTextEdit 额外支持富文本编辑(如设置字体颜色、插入图片等)。
QTextBrowser:
默认只读(readOnly 属性默认 true),虽然可通过 setReadOnly(false) 强制开启编辑,但设计初衷是 “浏览” 而非 “编辑”,编辑体验较差(如富文本编辑功能被弱化)。
注意:ui->textBrowser->setReadOnly(false); 设置为false也是可以编辑的
3.附加功能
QPlainTextEdit:
专注纯文本效率,提供代码编辑优化:
支持行号显示(需配合 QTextEdit 或自定义实现)。
低延迟滚动(处理 10 万行以上文本时,性能优于 QTextEdit)。
支持 “按列选择”(按住 Alt 键拖动鼠标)。
QTextEdit:
专注富文本处理,提供格式控制接口:
通过 setFont()、setTextColor() 等方法设置文本样式。
通过 insertHtml()、insertImage() 插入富文本内容。
支持段落对齐(左对齐、居中、右对齐)。
QTextBrowser:
专注文本浏览与交互,扩展导航功能:
支持超链接(<a href="xxx">链接</a>),点击链接可触发 anchorClicked 信号(如跳转到其他页面)。
内置 “前进 / 后退” 导航(forward()、backward() 方法),类似浏览器历史记录。
可加载本地 HTML 文件(setSource(QUrl) 方法),适合展示帮助文档。
三.选择指南
只需要显示纯文本或日志 → 选择 QPlainTextEdit
需要富文本编辑功能 → 选择 QTextEdit
需要显示富文本内容但不需编辑 → 选择 QTextBrowser
处理大量文本(>10,000行) → 优先选择 QPlainTextEdit
需要超链接功能 → 选择 QTextEdit 或 QTextBrowser
需要文本浏览历史 → 选择 QTextBrowser
- 测试代码示例
1.代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//可读、可写设置
//ui->textEdit->setReadOnly(true);
//ui->plainTextEdit->setReadOnly(true);
ui->textBrowser->setReadOnly(false); //默认就是只读的,false就是可写的
//设置等宽字体,便于对齐
QFont font("Lucida Console", 10); //等宽字体 //粗一点
//QFont font("Courier New", 10); // 等宽字体 //字体很细
ui->textBrowser->setFont(font);
ui->textEdit->setFont(font);
ui->plainTextEdit->setFont(font);
//显示数据
QString tem = QString::number(1236).rightJustified(10, ' ');
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
tem = QString::number(123567).rightJustified(10, ' ');
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
tem = QString::number(12).rightJustified(10, ' ');
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
//网页格式方式,plainTextEdit不支持;语句自动换行
tem = "<font color=\"red\">QT软件</font> <b>QT测试</b>";
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
tem = "<font color=\"red\">QT软件</font> <b>QT测试</b>";
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
tem = "<span style=\"color: red; font-weight: bold;\">QT软件</span> "
"<span style=\"color: red; font-weight: bold;\">QT测试</span>";
ui->textBrowser->append(tem);
ui->textEdit->append(tem);
ui->plainTextEdit->appendPlainText(tem);
//三个控件都可以用的写入格式字符串的通用方法
insertFormattedText(ui->textBrowser, "\r\nTest Result:", Qt::black, true, false); //需要前面加入\r\n 手动换行
insertFormattedText(ui->textEdit, "\r\nTest Result:", Qt::black, true, false);
insertFormattedText(ui->plainTextEdit, "\r\nTest Result:", Qt::black, true, false);
}
void MainWindow::insertFormattedText(QTextEdit *editor, const QString &text,
const QColor &color,
bool bold,
bool italic)
{
QTextCursor cursor = editor->textCursor();
QTextCharFormat format;
QTextCharFormat originalFormat = cursor.charFormat();
// 移动到文档末尾
cursor.movePosition(QTextCursor::End);
// 设置文本颜色
format.setForeground(color);
// 设置加粗
format.setFontWeight(bold ? QFont::Bold : QFont::Normal);
// 设置斜体
format.setFontItalic(italic);
// 应用格式
cursor.mergeCharFormat(format);
// 插入文本
cursor.insertText(text);
// 恢复默认格式(可选)
cursor.setCharFormat(originalFormat);
// 将修改后的光标设置回编辑器
editor->setTextCursor(cursor);
}
void MainWindow::insertFormattedText(QPlainTextEdit *editor, const QString &text,
const QColor &color,
bool bold,
bool italic)
{
QTextCursor cursor = editor->textCursor();
QTextCharFormat format;
QTextCharFormat originalFormat = cursor.charFormat();
// 移动到文档末尾
cursor.movePosition(QTextCursor::End);
// 设置文本颜色
format.setForeground(color);
// 设置加粗
format.setFontWeight(bold ? QFont::Bold : QFont::Normal);
// 设置斜体
format.setFontItalic(italic);
// 应用格式
cursor.mergeCharFormat(format);
// 插入文本
cursor.insertText(text);
// 恢复默认格式(可选)
cursor.setCharFormat(originalFormat);
// 将修改后的光标设置回编辑器
editor->setTextCursor(cursor);
}
2.运行效果