font
| API | 说明 |
|---|---|
| font() | 获取当前widget的字体信息.返回QFont对象. |
| setFont(const QFont& font) | 设置当前widget的字体信息. |
| 属性 | 说明 |
|---|---|
| family | 字体家族.⽐如"楷体",“宋体”,"微软雅⿊"等. |
| pointSize | 字体⼤⼩ |
| weight | 字体粗细.以数值⽅式表⽰粗细程度取值范围为[0,99],数值越⼤,越粗. |
| bold | 是否加粗.设置为true,相当于weight为75.设置为false相当于weight为50. |
| italic | 是否倾斜 |
| underline | 是否带有下划线 |
| strikeOut | 是否带有删除线 |
代码⽰例:在Qt Designer中设置字体属性
1)在界⾯上创建⼀个label
![![[Pasted image 20250419105315.png]]](https://i-blog.csdnimg.cn/direct/0aa32482e6524926b8cf1595590a08c1.png)
2)在右侧的属性编辑区,设置该label的font相关属性
在这⾥调整上述属性,可以实时的看到⽂字的变化.
![![[Pasted image 20250419105403.png]]](https://i-blog.csdnimg.cn/direct/ea94586f54384780a36d1a9b833df29c.png)
3)执⾏程序,观察效果
![![[Pasted image 20250419105811.png]]](https://i-blog.csdnimg.cn/direct/9aefada9b8b3437598a8d1f0a0f3577c.png)
通过属性编辑这样的方式,虽然能够快速方便的修改文字相关的属性,但是还不够灵活如果程序运行过程中,需要修改文字相关的属性~~就需要通过代码来操作了.
代码⽰例:在代码中设置字体属性
#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QLabel* label = new QLabel(this);
label->setText("这是一段文本");
//创建字体对象
QFont font;
font.setFamily("仿宋");
font.setPixelSize(30);
font.setBold(true);
font.setItalic(true);
font.setUnderline(true);
font.setStrikeOut(true);
//把font对象设置到label中
labe

最低0.47元/天 解锁文章
542

被折叠的 条评论
为什么被折叠?



