Qt5创建标准字体对话框(QFontDialog类)

接续上节:https://blog.csdn.net/xunye_dream/article/details/83003316

头文件:

#ifndef DIALOG_H
#define DIALOG_H

#include 

class QPushButton;
class QLineEdit;
class QGridLayout;
class QFrame;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    void createFileDialog();
    void createColorDialog();
    void createFontDialog();
    void addGridLayout();
    void registerConnect();

private slots:
    void showFile();
    void showColor();
    void showFont();

private:
    //file dialog
    QPushButton *fileBtn;
    QLineEdit   *fileLineEdit;
    //color dialog
    QPushButton *colorBtn;
    QFrame *colorFrame;
    //font dialog
    QPushButton *fontBtn;
    QLineEdit *fontLineEdit;

    // layout
    QGridLayout *mainLayout;
};

#endif // DIALOG_H

源文件:

#include "dialog.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("各种标准对话框的实例"));
    createFileDialog();
    createColorDialog();
    createFontDialog();
    addGridLayout();
    registerConnect();
}

void Dialog::createFileDialog()
{
    fileBtn = new QPushButton;
    fileBtn->setText(tr("文本标准对话框实例"));
    fileLineEdit = new QLineEdit;
}

void Dialog::createColorDialog()
{
    colorBtn = new QPushButton;
    colorBtn->setText(tr("颜色标准对话框实例"));
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);
}

void Dialog::createFontDialog()
{
    fontBtn = new QPushButton;
    fontBtn->setText(tr("字体标准对话框实例"));
    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Welcome!"));
}

void Dialog::addGridLayout()
{
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileBtn, 0, 0);
    mainLayout->addWidget(fileLineEdit, 0, 1);
    mainLayout->addWidget(colorBtn, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    mainLayout->addWidget(fontBtn, 2, 0);
    mainLayout->addWidget(fontLineEdit, 2, 1);
}

void Dialog::showFile()
{
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/",
                    "C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
    fileLineEdit->setText(s);
}

void Dialog::showColor()
{
    QColor c = QColorDialog::getColor(Qt::blue);
    if (c.isValid())
    {
        colorFrame->setPalette(QPalette(c));
    }
}

void Dialog::showFont()
{
    bool ok = true;
    QFont f = QFontDialog::getFont(&ok);
    if (ok)
    {
        fontLineEdit->setFont(f);
    }
}

void Dialog::registerConnect()
{
    connect(fileBtn, SIGNAL(clicked()), this, SLOT(showFile()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT(showColor()));
    connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
}

Dialog::~Dialog()
{
    if (fileBtn) delete fileBtn;
    if (fileLineEdit) delete fileLineEdit;
    if (colorBtn) delete colorBtn;
    if (colorFrame) delete colorFrame;
    if (fontBtn) delete fontBtn;
    if (fontLineEdit) delete fontLineEdit;
    if (mainLayout) delete mainLayout;
}

显示结果:

Qt5创建标准字体对话框(QFontDialog类)_第1张图片

点击字体标准对话框实例按钮,显示结果:

Qt5创建标准字体对话框(QFontDialog类)_第2张图片

你可能感兴趣的:(Qt,编码,QFontDialog,Font,Qt5)