dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLineEdit>
#include <QGridLayout>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QPushButton *fileBtn;
QLineEdit *fileLineEdit;
QGridLayout *mainLayout;
//
QPushButton *colorBtn;
QFrame *colorFrame;
//
QPushButton *fontBtn;
QLineEdit *fontLineEdit;
private slots:
void showFile();
void showColor();
void showFont();
};
#endif // DIALOG_H
dialog.app
#include "dialog.h"
#include <QPushButton>
#include <QGridLayout>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
fileBtn=new QPushButton; //各个控件对象的初始化
fileBtn->setText(tr("文件标准对话框实例"));
fileLineEdit=new QLineEdit;
mainLayout=new QGridLayout(this);
mainLayout->addWidget(fileBtn,0,0);
mainLayout->addWidget(fileLineEdit,0,1);
connect(fileBtn,
SIGNAL(clicked()),
this,
SLOT(showFile()));
//Color Button
colorBtn = new QPushButton;
colorBtn->setText("颜色标准对话框实例");
colorFrame= new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
mainLayout->addWidget(colorBtn,1,0);
mainLayout->addWidget(colorFrame,1,1);
connect(colorBtn,
SIGNAL(clicked()),
this,
SLOT(showColor()));
//Font
fontBtn = new QPushButton;
fontBtn->setText("字体标准对话框实例");
fontLineEdit = new QLineEdit();
fontLineEdit->setText("Welcome!");
mainLayout->addWidget(fontBtn,2,0);
mainLayout->addWidget(fontLineEdit,2,1);
connect(fontBtn,
SIGNAL(clicked()),
this,
SLOT(showFont()));
}
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 ;
QFont f =QFontDialog::getFont(&ok);
if(ok)
{
fontLineEdit->setFont(f);
}
}
Dialog::~Dialog()
{
}