You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义QTextDocument中特定行之间的间距?

解决第一行与第二行自定义间距的问题

你之前的问题出在**line-height属性的使用对象错误**:<span>是行内元素,设置line-height只会影响该行内部的行高,无法控制段落与下一行的间距。要实现第一行和第二行的自定义间距,应该调整第一段落的外边距(margin),而非行内元素的行高。

修改后的代码示例

方案1:直接调整<p>的底部外边距,保留原有<br>结构

QTextEdit* textEdit = new QTextEdit;
QTextDocument* doc = new QTextDocument;
QString html = R"(
<p style="margin-bottom: 10px;"><font style="font-size: 11px; color: rgba(255, 0, 0, 255);"> world — 05/04/2024 3:15 PM </font></p>
green<br>blue
)";

doc->setHtml(html);

方案2:用<p>统一包裹所有行,精准控制各段落间距

QTextEdit* textEdit = new QTextEdit;
QTextDocument* doc = new QTextDocument;
QString html = R"(
<p style="margin-bottom: 10px;"><font style="font-size: 11px; color: rgba(255, 0, 0, 255);"> world — 05/04/2024 3:15 PM </font></p>
<p style="margin: 0;">green</p>
<p style="margin: 0;">blue</p>
)";

doc->setHtml(html);

原理说明

  • <p>标签默认带有上下外边距,通过设置margin-bottom可以直接控制第一段落与下一行的间距。
  • 若后续行也使用<p>,设置margin: 0就能让它们保持紧凑的一致行距,避免默认间距干扰。

内容的提问来源于stack exchange,提问作者Ruan Sebastian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 12:21:21