QT标准对话框

初学 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();

}


 

程序运行截图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值