头文件中用class editPainter比#include "editpainter.h"好多少
头文件中用class editPainter比#include "editpainter.h"好多少
#include <QtGui/QWidget> class QPushButton; class editPainter; class QLabel; class QGroupBox; class QRadioButton; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private slots: void OnClickImport(); void OnClickApply(); void OnPixmapChange(QPixmap *pixmap); void OnRadioFixClick(); void OnRadioFreeClick(); private: QPushButton *m_pButtonImport; QPushButton *m_pButtonApply; editPainter *m_pPhotoEditor; QLabel *m_pLabelPixmap; QGroupBox *m_pGrpBoxScaleType; QRadioButton *m_pRadioFix; QRadioButton *m_pRadioFree; QPixmap *m_pPixmapTobesaved; };
#include "editpainter.h" #include <QtGui/QPushButton> #include <QtGui/QLabel> #include <QtGui/QGroupBox> #include <QtGui/QRadioButton> #include <QFileDialog> #include <QDateTime> Widget::Widget(QWidget *parent) : QWidget(parent) , m_pButtonImport(NULL) , m_pButtonApply(NULL) , m_pPhotoEditor(NULL) , m_pLabelPixmap(NULL) , m_pGrpBoxScaleType(NULL) , m_pRadioFix(NULL) , m_pRadioFree(NULL) , m_pPixmapTobesaved(NULL) { m_pButtonImport = new QPushButton("import", this); m_pButtonApply = new QPushButton("apply", this); m_pPhotoEditor = new editPainter(this); m_pLabelPixmap = new QLabel(this); m_pGrpBoxScaleType = new QGroupBox(this); m_pRadioFix = new QRadioButton("fix", m_pGrpBoxScaleType); m_pRadioFree = new QRadioButton("free", m_pGrpBoxScaleType); m_pRadioFix->setChecked(true); m_pButtonImport->setGeometry(300, 0, 100, 285); m_pButtonApply->setGeometry(400, 175, 98, 110); m_pPhotoEditor->setGeometry(0, 0, 300, 285); m_pLabelPixmap->setGeometry(400, 0, 98, 125); m_pGrpBoxScaleType->setGeometry(400, 125, 98, 50); m_pRadioFix->setGeometry(5, 5, 60, 20); m_pRadioFree->setGeometry(5, 25, 60, 20); connect(m_pButtonImport, SIGNAL(clicked()), this, SLOT(OnClickImport())); connect(m_pButtonApply, SIGNAL(clicked()), this, SLOT(OnClickApply())); connect(m_pPhotoEditor, SIGNAL(signalPixmapChange(QPixmap*)), this, SLOT(OnPixmapChange(QPixmap*))); connect(m_pRadioFix, SIGNAL(clicked()), this, SLOT(OnRadioFixClick())); connect(m_pRadioFree, SIGNAL(clicked()), this, SLOT(OnRadioFreeClick())); resize(498, 285); } Widget::~Widget() { } void Widget::OnClickImport() { QString strFileName = QFileDialog::getOpenFileName(this, tr("Import Image"), "./", "(*.png;*.gif;*.jpg;*.bmp)"); if (strFileName.isEmpty()) { return; } if (NULL != m_pPhotoEditor) { m_pPhotoEditor->setImagePath(strFileName); } } void Widget::OnClickApply() { if (NULL != m_pPixmapTobesaved) { QDateTime current = QDateTime::currentDateTime(); QString time = current.toString("yyyy_mm_dd_hh_ss_zzz"); QString fileName = "e:/photo/" + time + ".bmp"; QDir dir("e:/photo/"); if (!dir.exists()) { dir.setPath("e:/"); dir.mkdir("photo"); } bool ret = m_pPixmapTobesaved->save(fileName); if (ret) { } else { } } else { } } void Widget::OnPixmapChange(QPixmap *pixmap) { if ((NULL!=m_pLabelPixmap) && (NULL!=pixmap)) { m_pPixmapTobesaved = pixmap; m_pLabelPixmap->setPixmap(*pixmap); } } void Widget::OnRadioFixClick() { if (NULL != m_pPhotoEditor) { m_pPhotoEditor->setScaleType(SCALE_FIX); } } void Widget::OnRadioFreeClick() { if (NULL != m_pPhotoEditor) { m_pPhotoEditor->setScaleType(SCALE_FREE); } }
是可以节约ram还是rom呢? 在头文件中申明类,在实现文件中包含头文件,是为了减少编译连接的依赖,加快编译速度
在头文件中申明类,在实现文件中包含头文件,是为减少编译依赖,eg:
在A.h中用#include"B.h"的话,如果B.h发生了改变的话,A.h和所有#include"A.h"的文件都要重新编译一下。。。
但是如果在A.h中用class B的话,当然这种形式只能声明存在一个类B,此时如果B.h发生变化的话,A.h和所有#include"A.h"的文件就不需要重新编译了,需要编译的只有#include"B.h"的。。。
假设有多个类需要调用B中的方法,当B发生修改时,第二种情况的编译效率要远远高于第一个。。。。
减少编译连接的依赖,加快编译速度