我想将我的文字放入QLabel
介于粗体和正常样式之间,我相信设置字体粗细应该是我问题的答案。
在 Qt 文档中,我发现有两个选项可以更改字体粗细:
-
从 cpp 端通过:QFont::setWeight()
接受数字 0-99 的方法
http://doc.qt.io/qt-4.8/qfont.html#Weight-enum http://doc.qt.io/qt-4.8/qfont.html#Weight-enum
-
来自 Qss 风格:font-weight
属性,接受数字 100,200,...,900
http://doc.qt.io/qt-4.8/stylesheet-reference.html#font-weight http://doc.qt.io/qt-4.8/stylesheet-reference.html#font-weight
我已经尝试了两种方法,但似乎没有任何效果。我总是只得到普通或普通的大胆风格,没有介于两者之间的风格。
Example:
QLabel* test1 = new QLabel("Font-weight testing");
test1->show();
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(40);
test2->setFont(font);
test2->show();
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("font-weight: 400");
test3->show();
在上面的示例中,我创建了 3 个标签。一种没有任何额外的设置,一种我通过更改字体粗细setWeight
方法,以及应通过 Qss 样式更改字体粗细的方法。但这三者最终将完全相同。
我什至尝试过增大字体、启用抗锯齿或使用不同的字体,但没有任何帮助。
The QFont::setWeight
方法期望其输入值是其中之一QFont::Weight
枚举值。
http://doc.qt.io/qt-5/qfont.html#setWeight http://doc.qt.io/qt-5/qfont.html#setWeight
正确的版本:
QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(QFont::Bold);
test2->setFont(font);
QSS 版本中还有两个错误。首先,您没有为规则指定选择器。其次,值 400 对应于“正常”字体。
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
正确的版本:
QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("QLabel { font-weight: bold; }");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)