// standarddialogs.h
#ifndef STANDARDDIALOGS_H
#define STANDARDDIALOGS_H
#include <QPushButton>
#include <QDialog>
#include <QWidget>
#include <QGridLayout>
#include <QLineEdit>
#include <QFrame>
#include <QColorDialog>
#include <QColor>
#include <QFont>
#include <QFontDialog>
#include <QString>
#include <QFileDialog>
class
StandardDialogs: public
QDialog //继承QDialog 对话框
{
Q_OBJECT // qt特色之一,据传有点类似宏,又比宏高级,有一个名称叫做meta object,暂时不清楚其中奥妙
public :
StandardDialogs(QWidget *parent = 0, Qt::WindowFlags f = 0);
~StandardDialogs();
public :
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
// standarddialogs.cpp
#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上布局
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()));
}
StandardDialogs::~StandardDialogs()
{
delete
layout;
delete
filePushButton;
delete
colorPushButton;
delete
fontPushButton;
delete
fileLineEdit;
delete
colorFrame;
delete
fontLineEdit;
}
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);
}
}
void
StandardDialogs::slotOpenFileDlg()
{
QString s = QFileDialog::getOpenFileName(
this ,
"open file dialog" ,
"/" ,
"C++ files(*.cpp);; C files(*.c);; Head File(*.h)" );
fileLineEdit->setText(s.toAscii());
}
// main.cpp
#include <QApplication>
#include <QPushButton>
#include "standarddialogs.h"
int
main( int
argc, char
*argv[])
{
QApplication a(argc, argv);
StandardDialogs ct;
ct.show();
QPushButton b( "Hello World!" );
b.show();
QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
return
a.exec();
}
|