1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <QPlainTextEdit> #include <QVBoxLayout> #include <QPushButton> #include <QLabel> #include <QTime> #include <qapplication.h>
void setupPlainTextEditDemo(QWidget *parent) { QVBoxLayout *layout = new QVBoxLayout(parent);
QPlainTextEdit *plainEdit = new QPlainTextEdit(parent); plainEdit->setPlaceholderText("这里是纯文本模式(适合显示大批量日志或代码)...");
plainEdit->setMaximumBlockCount(100);
QPushButton *btnLog = new QPushButton("模拟产生一条日志", parent); QObject::connect(btnLog, &QPushButton::clicked, [plainEdit]() { QString timeStr = QTime::currentTime().toString("hh:mm:ss.zzz"); plainEdit->appendPlainText(QString("[%1] 系统运行正常,数据处理中...").arg(timeStr)); });
QLabel *lineCountLabel = new QLabel("当前行数:1", parent); QObject::connect(plainEdit, &QPlainTextEdit::blockCountChanged, [lineCountLabel](int newBlockCount) { lineCountLabel->setText(QString("当前行数:%1").arg(newBlockCount)); });
QPushButton *btnReadOnly = new QPushButton("切换只读/编辑状态", parent); btnReadOnly->setCheckable(true); QObject::connect(btnReadOnly, &QPushButton::toggled, [plainEdit](bool checked) { plainEdit->setReadOnly(checked); plainEdit->setStyleSheet(checked ? "background-color: #f0f0f0;" : ""); });
layout->addWidget(new QLabel("日志/纯文本编辑器:")); layout->addWidget(plainEdit); layout->addWidget(lineCountLabel); layout->addWidget(btnLog); layout->addWidget(btnReadOnly); } int main(int argc, char *argv[]) { QApplication a(argc, argv);
QWidget w; w.setWindowTitle("QLineEdit 深度测试"); w.resize(400, 300);
setupPlainTextEditDemo(&w);
w.show(); return a.exec(); }
|