初学
QT
,完全没有一点概念,在一本书上面看了几遍代码,刚刚对
QT
有一个简单的认识,与大家分享一下书上面的代码
//CommonDialog.h
#ifndef COMMONDIALOG_H
#define COMMONDIALOG_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);
~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 // COMMONDIALOG_H
//CommonDialog.cpp
#include "CommonDialog.h"
#include <QtGui>
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()));
}
StandardDialogs::~StandardDialogs()
{
}
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);
}
}
//main.cpp
#include <QApplication>
#include "CommonDialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
StandardDialogs *sd = new StandardDialogs;
sd->show();
return app.exec();
}
程序运行截图: