• Qt5开发从入门到精通——第四篇(标准字体对话框类 getFont())


    欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
    博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


    前言

    本章节将会给大家带来标准字体对话框QFontDialog 类的详细使用方法


    一、getFont()函数说明

    getFontO 函数是标准字体对话框 QFontDialog 类的 一个静态函数,该函数返回用户所选择的字体,下面是 getFontO函数形式:

    QFont getFont
    {
    	bool *ok,               //注
    	QWidget *parent = 0    //标准字体对话框的父窗口
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注:若用户单击 “OK” 按钮,则该参数*ok 将设为 true, 函数返回用户所选择的字体;否则,将设为 false,此时函数返回默认字体。

    二、效果实例

    图一
    在这里插入图片描述

    三、原码详解

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = nullptr);
        ~Dialog();
    
    private:
        Ui::Dialog *ui;
        QPushButton *fontBtn;
        QLineEdit *fontLineEdit;
        QGridLayout *mainLayout;
    private slots:
    void showFont();
    
    };
    
    #endif // DIALOG_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        fontBtn = new QPushButton;
        fontBtn->setText(tr("字体标准对话框实例"));
        fontLineEdit = new QLineEdit;
        fontLineEdit->setText(tr("Welcome!"));   //显示更改的字符串
        mainLayout = new QGridLayout(this);
        mainLayout->addWidget(fontBtn,2,0);     //布局设计
        mainLayout->addWidget(fontLineEdit,2,1);
        connect(fontBtn,SIGNAL(clicked()),this ,SLOT(showFont()));  // 事件关联
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    
    void Dialog::showFont()
    {
        bool ok;
        QFont f = QFontDialog::getFont(&ok);
        if (ok)
        {
            fontLineEdit->setFont(f);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    main.cpp

    #include "dialog.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    总结

    标准字体对话框 getFont()函数也是在应用程序中经常用到的

  • 相关阅读:
    vue3+ts项目04-国际化
    海思3559万能平台搭建:RTSP实时播放的优化
    老板让我牵头搞ELK,我该如何确定ES的集群规模?
    专用/独享代理与共享代理有何区别?如何选择?
    浅谈非线性回归(non-linear regression)
    如何对待工作中的失误
    SpringMVC中的请求重定向和转发
    2022-05-05 mybatis-plus 批量插入修改操作
    计算机毕业设计django基于python街区医院管理系统
    网络七层结构(讲人话)
  • 原文地址:https://blog.csdn.net/weixin_44759598/article/details/126561442