思路
设置当前行文本的格式 需要使用到QTextCharFormat类,设置它的文本颜色或者文本字体,然后并获取 当前文本的光标,从光标开始位置设置文本的字符格式。
代码
1、设置文本当前行的颜色
void MainWindow::setInsertTextColor(const QColor &color)
{
QTextCharFormat fmt;//文本字符格式
fmt.setForeground(color);// 前景色(即字体色)设为color色
QTextCursor cursor = ui->infoTextEdit->textCursor();//获取文本光标
cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示
ui->infoTextEdit->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式
}
2、设置文本当前行的字体
void MainWindow::setInsertTextFont(const QFont &font)
{
QTextCharFormat fmt;//文本字符格式
fmt.setFont(font);//字体
QTextCursor cursor = ui->m_textdisplay->textCursor();//获取文本光标
cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示
ui->m_textdisplay->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式
}
3、使用总结
//设置时间颜色与字体
setInsertTextFont(timeFont);
setInsertTextColor(timeColor);
ui->infoText->append(curTime.toString());