qt TextEdit 带色操作

void MainWindow::showTip_Rev(QString text, QColor color)
{
    QDateTime *datatime = new QDateTime(QDateTime::currentDateTime()); // 系统时间
    QString str_time = datatime->toString("yyyy-MM-dd hh:mm:ss:zzz"); //设置显示格式
    ui->textEdit_ShowBoard->append(" -> " + str_time + ": 0x"+ text);
    QTextCursor cursor = ui->textEdit_ShowBoard->textCursor();
    cursor.select(QTextCursor::BlockUnderCursor);   //选中要着色的内容
    QTextCharFormat fmt;
    fmt.setForeground(color);
    cursor.mergeCharFormat(fmt);    //设置文本格式
    cursor.clearSelection();        //撤销选中
    cursor.movePosition(QTextCursor::EndOfLine);  //cursor和anchor均移至末尾
}
QString m = "abcdef";
showTip_Rev(m, Qt::red);
showTip_Rev(m, Qt::black);

 

你可能感兴趣的:(Qt工程)