记录QTextEdit

一、代码如下:

    //设置行间距
    QTextBlockFormat blockFormat;
    blockFormat.setLineHeight(10, QTextBlockFormat::LineDistanceHeight);
    auto textCursor = ui->textEdit->textCursor();
    textCursor.setBlockFormat(blockFormat);
    ui->textEdit->setTextCursor(textCursor);
    //注意:append和insertHtml函数无法在首行加上空行(会自动去除空白字符串。),文本中间的多个空白字符串会被全部删除或保留一个。
//    ui->textEdit->setLineWrapMode(QTextEdit::NoWrap); //文本不换行
//    ui->textEdit->setReadOnly(true); //日志框设置只读属性
    QString title= "日志信息";
    QString info1 = "2020.01.01 20:15:38    启动软件,系统初始化                         a";
    QString info2 = "2020.01.01 20:15:38    软件初始化完成,准备连接服务器";
    QString info3 = "2020.01.01 20:15:38    软件已就绪";
    QString info4 = "2020.01.01 20:15:38    变频接收机1输入通道故障,故障码404";
    QString info5 = "2020.01.01 20:15:38    变频接收机1输出通道1故障,故障码405";
    QString info6 = "2020.01.01 20:15:38    滤波器输出通道7离线中";
    QString info7 = "2020.01.01 20:15:38    滤波器输出通道8离线中";
    ui->textEdit->insertPlainText(" \n"); //可以读取换行符号
    ui->textEdit->insertPlainText("hah   hsjhd"); //从当前光标处插入文本,可以插入空白字符,且不会被省略
    ui->textEdit->append(QString("<font size=16px color=\"#00DBFF\">%1</font>").arg(title));
    ui->textEdit->append(QString("<font size=20px color=\"Qt::blue\"></font>")); //空行
    ui->textEdit->append(QString("<font size=14px color=\"Qt::blue\">%1</font>").arg(info1));
    ui->textEdit->append(QString("<font size=14px color=\"Qt::blue\">%1</font>").arg(info2));
    ui->textEdit->append(QString("<font size=14px color=\"Qt::blue\">%1</font>").arg(info3));
    ui->textEdit->append(QString("<font size=14px color=\"#FF0000\">%1</font>").arg(info4));
    ui->textEdit->append(QString("<font size=14px color=\"#FF0000\">%1</font>").arg(info5));
    ui->textEdit->insertHtml("<p></p>"); //空行
    ui->textEdit->append(QString("<font size=14px color=\"#E3E512\">%1</font>").arg(info6));

二、运行结果:

posted @ 2020-10-28 23:29  xianyongchao  阅读(407)  评论(0编辑  收藏  举报