小智 6
使用HTML文字:
QString template = "<p style=\"line-height:%1%\">%2<p>";
QString targetText = template.arg(myPercentage).arg(myTex);
QLabel *l = new QLabel(targetText, this);
Run Code Online (Sandbox Code Playgroud)
其中myPercentage大约为60-80。在“自动换行”模式下,您将获得压缩行
中没有行间距属性QLabel。您可以更改小部件字体,这将更改行的高度,但我怀疑这不是您想要的。
行高是从小QFont部件的计算出来的,可以通过QFontMetrics与小部件关联的获得。使用这些信息,您可以创建自己的具有行间距属性(和文本换行模式)的小部件,但这代表了许多低级工作。
您也可以QLabel直接在 Qt Designer 中编辑 的 HTML 。
QLabel部分下,选择text属性并按下...按钮。下面是两个QLabel使用 HTML控制行间距的示例(在 Qt 5.7 中测试)。我确信还有更多(和一些更好的)编写 HTML 的方法,但这应该是一个好的开始。
示例 1
<html><head/><body>
<p style="line-height:120"><span>
This is the first line of the label.<br>
This is the second line.<br>
This is the third and final line.
</span></p>
</body></html>
Run Code Online (Sandbox Code Playgroud)
如果整个段落的行距相同,则此示例更简洁。
示例 2
<html><head/><body>
<p style="line-height:20"><span>This is the first line of the label.</span></p>
<p style="line-height:20"><span>This is the second line.</span></p>
<p style="line-height:100"><span>This is the third and final line.</span></p>
</body></html>
Run Code Online (Sandbox Code Playgroud)
此示例允许您单独控制每行的间距。我不得不将最后一行的高度设为 100,以防止 Qt 将其切成两半。我认为它会影响 Qt 作为小部件计算标签高度的方式。
| 归档时间: |
|
| 查看次数: |
5209 次 |
| 最近记录: |