在WordWrap模式下,如何在QLabel中设置行高?
答案 0 :(得分:4)
QLabel
中没有行间距属性。您可以更改小部件字体,这将改变线条的高度,但我怀疑这不是您想要的。
行高是根据窗口小部件的QFont
计算的,可以通过与窗口小部件关联的QFontMetrics
获取。使用此信息,您可以创建自己的具有行间距属性(和文本换行模式)的窗口小部件,但这代表了许多低级别的工作。
答案 1 :(得分:4)
使用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模式下获得压缩线
答案 2 :(得分:1)
您也可以直接在Qt Designer中编辑QLabel
的HTML。
QLabel
部分下,选择text
属性,然后按...
按钮。以下是使用HTML控制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如何计算标签作为小部件的高度。