QT6 源,七章对话框与多窗体(7) 字体对话框 QFontDialog :本类的属性,信号函数,静态成员函数与源代码带注释

(1)本 QT 的预定义字体对话框,只是为了获得字体的设置。用该类的静态成员函数就可以了
本类很相似于颜色对话框 QColorDialog

在这里插入图片描述

++

在这里插入图片描述

++

在这里插入图片描述

(2)接着介绍本类的静态成员函数

在这里插入图片描述

++

在这里插入图片描述

++测试一下

在这里插入图片描述

(3)本类定义于头文件 qfontdialog . h

#ifndef QFONTDIALOG_H
#define QFONTDIALOG_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qwindowdefs.h>
#include <QtGui/qfont.h>

#include <QtWidgets/qdialog.h>

QT_REQUIRE_CONFIG(fontdialog);

QT_BEGIN_NAMESPACE

class QFontDialogPrivate;

/*
The QFontDialog class provides a dialog widget for selecting a font.

Detailed Description  :
通过静态的函数之一getFont()创建一个字体对话框。
     bool ok;
     QFont font = QFontDialog::getFont(
                     &ok, QFont("Helvetica [Cronyx]", 10), this);
     if (ok) {
         // the user clicked OK and font is set to the font the user selected
     } else {
         // the user canceled the dialog; font is set to the initial
         // value, in this case Helvetica [Cronyx], 10
     }

对话框还可以用于直接设置小部件的字体:
     myWidget.setFont(QFontDialog::getFont(0, myWidget.font()));
如果用户点击确定,他们选择的字体将用于myWidget;如果他们点击取消,则使用原始字体。

*/

class Q_WIDGETS_EXPORT QFontDialog : public QDialog
{
    Q_OBJECT

    Q_DECLARE_PRIVATE(QFontDialog)

    //This property holds the current font of the dialog.
    Q_PROPERTY(QFont       currentFont
                READ       currentFont
                WRITE   setCurrentFont
                NOTIFY     currentFontChanged)

    Q_PROPERTY(FontDialogOptions  options  READ  options  WRITE  setOptions)

private:
    Q_DISABLE_COPY(QFontDialog)

    Q_PRIVATE_SLOT(d_func(), void _q_sizeChanged(const QString &))
    Q_PRIVATE_SLOT(d_func(), void _q_familyHighlighted(int))
    Q_PRIVATE_SLOT(d_func(), void _q_writingSystemHighlighted(int))
    Q_PRIVATE_SLOT(d_func(), void _q_styleHighlighted(int))
    Q_PRIVATE_SLOT(d_func(), void _q_sizeHighlighted(int))
    Q_PRIVATE_SLOT(d_func(), void _q_updateSample())

protected:
    bool       eventFilter(QObject * object, QEvent * event) override;
    void changeEvent      (                  QEvent * event) override;

    void done(int result) override;

Q_SIGNALS:
    //void currentFontChanged(const QFont & font); 这是个函数

    void          fontSelected(const QFont & font);
public:
    QFont             selectedFont() const;

    //将对话框显示为窗口模态 window modal dialog对话框,并立即返回。
    using QDialog::open; //virtual void QDialog::open(); 此处构成了函数重载。

    void  open(QObject * receiver, const char * member);
    //Opens the dialog and connects its fontSelected() signal to the
    //  slot specified by receiver and member.
    //The signal will be disconnected from the slot when the dialog is closed.

public:

    explicit QFontDialog(                       QWidget * parent = nullptr);

    explicit QFontDialog(const QFont & initial, QWidget * parent = nullptr);

    ~QFontDialog();


//   Q_PROPERTY(QFont      currentFont
//              READ       currentFont
//              WRITE   setCurrentFont
//              NOTIFY     currentFontChanged )
                QFont      currentFont() const;
                void    setCurrentFont       (const QFont & font);
Q_SIGNALS:
                void       currentFontChanged(const QFont & font);


public:

    //此枚举定义了影响字体对话框外观和感觉的各种选项。
    //例如,它允许指定应显示的字体类型。如果未指定任何内容,则将列出所有可用的字体。
    //请注意,字体过滤选项可能在某些平台上(如Mac)可能无法支持。
    //这些选项在非原生对话框中(用于Windows或Linux)总是支持的。
    enum FontDialogOption {
        NoButtons           = 0x00000001, //不显示确定和取消按钮。(适用于“实时对话框)。
        DontUseNativeDialog = 0x00000002,
        //在Mac上使用Qt的标准字体对话框,而不是Apple的原生字体面板。
            ScalableFonts   = 0x00000004, //Show     scalable fonts
         NonScalableFonts   = 0x00000008, //Show non scalable fonts
          MonospacedFonts   = 0x00000010, //Show monospaced   fonts
        ProportionalFonts   = 0x00000020  //Show proportional fonts。 比例的proportional
    };
    Q_ENUM(FontDialogOption)
    Q_DECLARE_FLAGS(FontDialogOptions, FontDialogOption)

//   Q_PROPERTY(FontDialogOptions      options
//              READ                   options           WRITE      setOptions)
                FontDialogOptions      options() const;
                void                setOptions(FontDialogOptions options);
                void                setOption (FontDialogOption  option, bool on = true);
                bool               testOption (FontDialogOption  option ) const;

    void setVisible(bool visible) override;

    /*
    这是一个重载函数。执行一个模态字体对话框并返回一个字体。
    如果用户点击确定,将返回所选字体。如果用户点击取消,则返回Qt默认字体。
    该对话框使用给定的父对象 parent构建。
    如果“ok”参数非空,那么当用户点击“确定”时,该参数所引用的值将被设置为 true,
    而当用户点击“取消”时,该值将被设置为 false。
    举例 :
         bool ok;
         QFont font = QFontDialog::getFont(&ok, this);
         if (ok) {
             // font is set to the font the user selected
         } else {
             // the user canceled the dialog; font is set to the default
             // application font, QApplication::font()
         }

    警告:在对话框执行过程中请勿删除父 parent。
    如果您想这么做,应使用 QFontDialog的构造函数之一自行创建对话框。
    */
    static QFont  getFont(  bool * ok,                        QWidget * parent = nullptr);

    static QFont  getFont(  bool * ok, const QFont & initial, QWidget * parent = nullptr,
                    const   QString           & title   = QString(),
                            FontDialogOptions   options = FontDialogOptions());
    /*
    执行一个模态字体对话框并返回一个字体。
    如果用户点击确定,将返回所选字体。如果用户点击取消,则返回初始字体 initial。
    该对话框由给定的父元素 parent 和选项中指定的选项 options构建。
    参数 title显示为对话框的窗口标题,而初始值 initial则是最初选定的字体。

    如果“ok”参数非空,
    那么当用户点击“确定”时,该参数所引用的值将被设为 true,
    而当用户点击“取消”时,该值将被设为 false。
         bool ok;
         QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
         if (ok) {
             // font is set to the font the user selected
         } else {
             // the user canceled the dialog;
             // font is set to the initial value, in this case Times, 12.
         }

    该对话框还可以用于直接设置小部件的字体 :
        myWidget.setFont(QFontDialog::getFont(0, myWidget.font()));
    在这个示例中,如果用户点击确定,则选择的字体将被使用;如果他们点击取消,则使用原始字体。

    警告:在对话框执行过程中请勿删除父 parent控件。
    如果您想这么做,应使用QFontDialog的构造函数之-自行创建对话框。
    */

}; //完结 class QFontDialog : public QDialog

Q_DECLARE_OPERATORS_FOR_FLAGS(QFontDialog::FontDialogOptions)

QT_END_NAMESPACE

#endif // QFONTDIALOG_H

(4)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangzhangkeji

谢谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值