首页 > 移动平台 > 详细

Qt QTextEdit/QTextBrowser append() 函数换行解决方法

时间:2020-12-10 17:39:05      阅读:352      评论:0      收藏:0      [点我收藏+]

解决方法

1 QTextCursor tc = ui->textRec->textCursor();
2 tc.movePosition(QTextCursor::End);
3 tc.insertText(appendStr);

更好的方法

这个方法参考了QT append()函数的源码。

 1     //获取滚动条位置
 2     bool atEnd = ui->textRec->verticalScrollBar()->value() >= ui->textRec->verticalScrollBar()->maximum();
 3     QTextCharFormat fmt;
 4     fmt.setForeground(color);
 5 
 6     QTextCursor tmp(ui->textRec->document());
 7 
 8     tmp.beginEditBlock();
 9     tmp.movePosition(QTextCursor::End);
10 
11     if (!ui->textRec->document()->isEmpty())
12     {
13         tmp.insertBlock(ui->textRec->textCursor().blockFormat(), ui->textRec->textCursor().charFormat());
14     }else{
15 
16         tmp.setCharFormat(ui->textRec->textCursor().charFormat());
17     }
18 
19     tmp.movePosition(QTextCursor::End);
20     tmp.deletePreviousChar();
21 
22     tmp.insertText(appendStr, fmt);
23 
24     // preserve the char format
25     QTextCharFormat oldCharFormat = ui->textRec->textCursor().charFormat();
26 
27     if (!ui->textRec->textCursor().hasSelection())
28         ui->textRec->textCursor().setCharFormat(oldCharFormat);
29 
30     tmp.endEditBlock();
31     if(atEnd)
32         ui->textRec->verticalScrollBar()->setValue(ui->textRec->verticalScrollBar()->maximum());

Qt QTextEdit/QTextBrowser append() 函数换行解决方法

原文:https://www.cnblogs.com/ybqjymy/p/14115554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!