文件、颜色、字体标准对话框使用

文件、颜色、字体标准对话框使用

1、 创建头文件standarddialogs.h

#ifndef STANDARDDIALOGS_H
#define STANDARDDIALOGS_H

#include <QDialog>

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

class StandardDialogs:public QDialog
{
    Q_OBJECT
public:
    StandardDialogs(QWidget *parent = 0,Qt::WindowFlags f=0);

    QGridLayout *layout;
    QPushButton *filePushButton;
    QPushButton *colorPushButton;
    QPushButton *fontPushButton;
    QLineEdit *fileLineEdit;
    QLineEdit *fontLineEdit;
    QFrame *colorFrame;

private slots:
    void slotOpenFileDlg();
    void slotOpenColorDlg();
    void slotOpenFontDlg();
};

#endif // STANDARDDIALOGS_H

2、创建standarddialogs.cpp文件。

#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
#include <QLineEdit>
#include <QFrame>
#include <QFileDialog>
#include <QFontDialog>
#include <QColorDialog>
#include <QColor>

#include "standarddialogs.h"

StandardDialogs::StandardDialogs(QWidget *parent,Qt::WindowFlags f):QDialog(parent, f)
{
    setWindowTitle(tr("Standard Dialogs"));

    layout = new QGridLayout(this);

    filePushButton = new QPushButton;
    filePushButton->setText(tr("File Dialog"));

    colorPushButton = new QPushButton;
    colorPushButton->setText(tr("Color Dialog"));

    fontPushButton = new QPushButton;
    fontPushButton->setText(tr("Font Dialog"));

    fileLineEdit = new QLineEdit;

    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);

    fontLineEdit = new QLineEdit;
    fontLineEdit->setText(tr("Hello World"));

    layout->addWidget(filePushButton,0,0);
    layout->addWidget(fileLineEdit,0,1);
    layout->addWidget(colorPushButton,1,0);
    layout->addWidget(colorFrame,1,1);
    layout->addWidget(fontPushButton,2,0);
    layout->addWidget(fontLineEdit,2,1);

    layout->setMargin(15);
    layout->setSpacing(10);

    connect(filePushButton,SIGNAL(clicked()),this,SLOT(slotOpenFileDlg()));
    connect(colorPushButton,SIGNAL(clicked()),this,SLOT(slotOpenColorDlg()));
    connect(fontPushButton,SIGNAL(clicked()),this,SLOT(slotOpenFontDlg()));
}

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

}

void StandardDialogs::slotOpenColorDlg()
{
    QColor color = QColorDialog::getColor(Qt::blue);

    if(color.isValid())
    {
        colorFrame->setPalette(QPalette(color));
    }
}

void StandardDialogs::slotOpenFontDlg()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok)
    {
        fontLineEdit->setFont(font);
    }
}

3、创建main.cpp文件。

#include <QApplication>
#include "standarddialogs.h"

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);

    StandardDialogs *standarddialog = new StandardDialogs;
    standarddialog->show();

    return app.exec();
}

4、运行程序
标准对话框

文件对话框

颜色对话框

字体对话框

5、 代码资源
代码资源下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值