qtextedit 如何修改默认的行间距?
qt并未提供直接的api来修改qtextedit的行间距,要做到修改行间距,可以通过两种方法:
1、使用QTextBlockFormat,代码如下:
QTextBlockFormat blockFormat;
blockFormat.setLineHeight(10, QTextBlockFormat::LineDistanceHeight);//设置行间距为10
ui->textEdit->selectAll();//选中全部文本,否则只会修改当前行
auto textCursor = ui->textEdit->textCursor();
textCursor.setBlockFormat(blockFormat);
ui->textEdit->setTextCursor(textCursor);
//设置完后,取消选中状态,若不写,则会导致存在选中状态
textCursor.clearSelection();
ui->textEdit->setTextCursor(textCursor);
这段代码可以在loaded等加载事件中使用。
2、可以在设置setHtml的时候,将文本包在一个html标签中,通过解析html来达到效果。
如下:
QString qsShow="<p style='line-height:10px; width:100% ; white-space: pre-wrap; '>" + yourcontent +"</p>";
此方式,可以参考该文档: