当QLabel处于换行模式时,我该如何设置行高?
使用HTML文本:
QString template = "<p style=\"line-height:%1%\">%2<p>";
QString targetText = template.arg(myPercentage).arg(myTex);
QLabel *l = new QLabel(targetText, this);
其中myPercentage的值在60-80之间。 在WordWrap模式下,您将获得压缩的行。
template
是一个不好的变量名。 - MikolasanQLabel
没有行间距属性。您可以更改小部件字体,这将更改行的高度,但我猜这不是您想要的。
行高是从窗口小部件的QFont
计算出来的,并可以通过与窗口小部件相关联的QFontMetrics
获得。使用此信息,您可以创建自己的小部件,该小部件具有行间距属性(以及文本换行模式),但这代表了大量的低级工作。
QLabel
的HTML。
QLabel
部分下,选择text
属性并按...
按钮。QLabel
行间距的示例(在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>
如果整个段落的行间距相同,那么这个例子看起来更加整洁。
例子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>
这个例子允许您单独控制每一行的间距。我不得不把最后一行的高度设为100,以防止Qt把它切成两半。我猜它会影响Qt将标签计算为小部件的高度。