文件、颜色、字体标准对话框使用
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、 代码资源
代码资源下载