Qt6.5类库详解:QFontComboBox

1 QFontComboBox介绍

QFontComboBox是Qt框架中的一个字体选择类,它提供了一个下拉列表框,用于选择字体。

  • QFontComboBox的主要功能和特点:

  • 字体选择:QFontComboBox提供了一个字体选择器,用户可以从下拉列表中选择不同的字体。
  • 字体筛选:可以使用setFontFilters()方法来设置字体筛选器,根据特定的筛选条件显示可用的字体。
  • 当前字体:可以通过setCurrentFont()方法来设置当前字体,也可以使用currentFont()方法来获取当前选择的字体。
  • 信号监听:QFontComboBox提供了currentFontChanged()信号,当选择的字体发生变化时,可以监听并响应这个信号。
  • 如何使用QFontComboBox:

头文件:#include <QFontComboBox>

cmake:find_package(Qt6 REQUIRED COMPONENTS Widgets)

target_link_libraries(mytarget PRIVATE Qt6::Widgets)

qmake:QT += widgets

继承于:QComboBox

2 QFontComboBox默认风格显示例子:

#include <QFontComboBox>
m_pFontComboBox = new QFontComboBox(this);
m_pFontComboBox->setGeometry(35, 35, 280, 34);

3 QFontComboBox信号

// 当前字体改变时,发射该信号。
void currentFontChanged(const QFont &f)

4 QFontComboBox属性和状态

  • fontFilters字体过滤器:

fontFilters字体过滤器,主要目的是确定哪些字体会出现在QFontComboBox的下拉列表中。

QFontComboBox::FontFilters宏定义如下:

QFontComboBox::AllFonts 0 显示所有字体。(默认)

QFontComboBox::ScalableFonts 0x1 可缩放字体。

QFontComboBox::NonScalableFonts 0x2 不可缩放字体。

QFontComboBox::MonospacedFonts 0x4 单间距字体。

QFontComboBox::ProportionalFonts 0x8 比例字体。

// 访问函数
void setFontFilters(QFontComboBox::FontFilters filters)
QFontComboBox::FontFilters fontFilters() const

/**** 例子: ****/
// 显示可缩放和不可缩放字体。
m_pFontComboBox->setFontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
  • currentFont当前字体:

// 访问函数
QFont currentFont() const
void setCurrentFont(const QFont &f)

/**** 例子: ****/
m_pFontComboBox->setCurrentFont(QFont("宋体"));

5 QFontComboBox完整示例

  • 示例执行效果:

  • .h文件:

#ifndef IMAINWINDOW_H
#define IMAINWINDOW_H

#include <QMainWindow>
#include <QFontComboBox>
#include <QLabel>

class IMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    IMainWindow(QWidget *parent = nullptr);
    ~IMainWindow();

private slots:
    void slotCurrentFontChanged(const QFont &f);

private:
    QLabel * m_pLabel1 {nullptr};
    QLabel * m_pLabel2 {nullptr};
    QLabel * m_pLabel3 {nullptr};
    QFontComboBox * m_pFontComboBox {nullptr};
};
#endif // IMAINWINDOW_H
  • .cpp文件:

#include "imainwindow.h"

#include <QLabel>

IMainWindow::IMainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setWindowTitle("欢迎关注公众号(20YC编程)");
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowMinMaxButtonsHint|Qt::WindowCloseButtonHint);
    this->resize(780, 380);

    // 创建QFontComboBox控件
    m_pFontComboBox = new QFontComboBox(this);
    m_pFontComboBox->setGeometry(35, 35, 280, 34);

    // 字体1:默认选择字体
    m_pLabel1 = new QLabel("字体1:默认选择字体", this);
    m_pLabel1->setGeometry(35, 75, 680, 32);

    // 字体2:选择字体 + 调整字体大小
    m_pLabel2 = new QLabel("字体2:选择字体 + 调整字体大小", this);
    m_pLabel2->setGeometry(35, 115, 680, 32);
    m_pLabel2->setStyleSheet("QLabel { color: green; }");

    // 字体3:选择字体 + 调整字体大小
    m_pLabel3 = new QLabel("字体3:选择字体 + 调整字体大小 + 斜体", this);
    m_pLabel3->setGeometry(35, 155, 680, 32);
    m_pLabel3->setStyleSheet("QLabel { color: red; }");

    connect(m_pFontComboBox, &QFontComboBox::currentFontChanged, this, &IMainWindow::slotCurrentFontChanged);
    m_pFontComboBox->setCurrentFont(QFont("宋体"));
}

IMainWindow::~IMainWindow()
{
}

void IMainWindow::slotCurrentFontChanged(const QFont &f)
{
    m_pLabel1->setFont(f);
    m_pLabel2->setFont(QFont(f.family(), 20));
    m_pLabel3->setFont(QFont(f.family(), 20, -1, true));
}
  • main.cpp文件:
#include "imainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    IMainWindow w;
    w.show();
    return a.exec();
}

-【End】-

#想了解更多精彩内容,关注下方公众号,还有示例源码、开发工具免费下载。

喜欢本文章,记得点赞、分享、关注哦~

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天恩软件工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值