Qt:更改字体粗细 [英] Qt: change font weight

查看:2457
本文介绍了Qt:更改字体粗细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的文字以QLabel介于粗体和普通样式之间,我相信设置字体粗细应该可以解决我的问题.

I would like to have my text in QLabel somewhere between bold and normal style and I believe that setting font-weight should be the answer to my problem.

在Qt文档中,我发现有两种方法可以更改字体粗细:

  1. 从cpp端通过:QFont::setWeight()接受数字0-99的方法

  1. From cpp side via: QFont::setWeight() method which accepts numbers 0-99

http://doc.qt.io/qt-4.8 /qfont.html#Weight-enum

通过Qss样式通过:font-weight属性,该属性接受数字100,200,...,900

From Qss style via: font-weight attribute, which accepts numbers 100,200,...,900

http://doc.qt.io/qt -4.8/stylesheet-reference.html#font-weight

我尝试了两种方法,但似乎没有任何效果.我总是只获得普通或普通的粗体样式,而在两者之间却一无所有.

I have tried both methods and nothing seems to work. I always get only normal or the ordinary bold style and nothing in between.

示例:

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样式更改了字体粗细.但是,这三个最终将完全相同.

In the example above, I have created 3 labels. One without any additional setting, one where I have changed font weight via setWeight method, and one where the font-weight should be changed via Qss style. But all three will end up being exactly the same.

我什至尝试使字体变大,启用抗锯齿或使用其他字体,但无济于事.

I have even tried to make font bigger, enable antialiasing, or use different font but nothing helped.

推荐答案

QFont::setWeight方法期望其输入值是QFont::Weight枚举值之一.

The QFont::setWeight method expects its input value to be one of the QFont::Weight enum values.

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对应于普通"字体.

Also you have two errors in the QSS version. First, you didn't specify a selector for your rule. Second, the value of 400 corresponds to 'normal' font.

https://developer.mozilla.org/en -US/docs/Web/CSS/font-weight

正确的版本:

QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("QLabel { font-weight: bold; }");

这篇关于Qt:更改字体粗细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆