While understanding the MVC pattern, I came across this particular article. I thought to implement it using the C++/QT library.
The intent over here is to implement/use MVC concepts in correct way. Awaiting the feedback/review comments from community.
model.h
#ifndef MODEL_H
#define MODEL_H
class Model {
public:
Model();
~Model();
double ConvertRuppesIntoDollor(double rs);
double GetDollorValue()const;
double GetRuppeValue() const;
void clear();
private:
double r;
double d;
};
#endif // MODEL_H
model.cpp
#include "model.h"
Model::Model(): r(),d() {}
Model::~Model(){}
double Model::ConvertRuppesIntoDollor(double rs) {
r = rs;
d = r/64.0;
return d;
}
double Model::GetDollorValue() const { return d;}
double Model::GetRuppeValue() const { return r;}
void Model::clear() {
r = 0;
d = 0;
}
view.h
#ifndef VIEW_H
#define VIEW_H
#include <QWidget>
#include <QString>
// Forward Declaration
class QPushButton;
class QLineEdit;
class QHBoxLayout;
class Controller;
class View : public QWidget {
Q_OBJECT
public:
explicit View(QWidget *parent = NULL, QString name = "MVC");
virtual ~View();
void setController(Controller* c);
QString getDollor();
QString getRuppes();
void setDollor(QString d);
void setRuppes(QString r);
public slots:
void ConvertButtonClicked();
void ClearButtonClicked();
private:
QPushButton* press;
QPushButton* clear;
QLineEdit* dollorinfo;
QLineEdit* ruppesinfo;
QHBoxLayout* hlayout;
Controller* controller;
QString appName;
QString dollor;
QString ruppes;
};
#endif // VIEW_H
view.cpp
#include "view.h"
#include "controller.h"
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <iostream>
View::View(QWidget *parent, QString name)
: QWidget(parent), appName(name) {
this->setWindowTitle(appName);
//Create a horizontal container widgets.
hlayout = new QHBoxLayout(this);
hlayout->setSpacing(1);
ruppesinfo = new QLineEdit(this);
ruppesinfo->setPlaceholderText("RuppesInfo");
dollorinfo = new QLineEdit(this);
dollorinfo->setPlaceholderText("DollorInfo");
QString buttonname = "Convert";
press = new QPushButton(buttonname, this);
QString clearname = "Clear";
clear = new QPushButton(clearname, this);
//Now add all child widgets inside parent one
hlayout->addWidget(ruppesinfo);
hlayout->addWidget(dollorinfo);
hlayout->addWidget(press);
hlayout->addWidget(clear);
//Connect the appropriate signal
connect(press, SIGNAL(clicked(bool)), this, SLOT(ConvertButtonClicked()));
connect(clear, SIGNAL(clicked(bool)), this, SLOT(ClearButtonClicked()));
}
View::~View(){}
void View::setController(Controller* c) {controller = c;}
QString View::getDollor() {return dollorinfo->text();}
QString View::getRuppes() {return ruppesinfo->text();}
void View::setDollor(QString d) {dollorinfo->setText(d);}
void View::setRuppes(QString r) {ruppesinfo->setText(r);}
void View::ConvertButtonClicked(){controller->OnConvertButtonClicked(this);}
void View::ClearButtonClicked() {controller->OnClearButtonClicked(this);}
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
//Forward Declaration
class Model;
class View;
class QString;
class Controller {
public:
Controller(Model* m);
virtual ~Controller();
void OnConvertButtonClicked(View* v);
void OnClearButtonClicked(View* v);
private:
Model* model;
};
#endif // CONTROLLER_H
controller.cpp
#include "controller.h"
#include "model.h"
#include "view.h"
#include <QString>
Controller::Controller(Model* m):model(m){}
Controller::~Controller(){}
void Controller::OnConvertButtonClicked(View* v) {
QString ruppes = v->getRuppes();
model->ConvertRuppesIntoDollor(ruppes.toFloat());
QString ds = QString::number(model->GetDollorValue());
v->setDollor(ds);
}
void Controller::OnClearButtonClicked(View* v) {
v->setDollor(QString());
v->setRuppes(QString());
model->clear();
}
client.cpp
#include <QApplication>
#include "view.h"
#include "model.h"
#include "controller.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Model model;
View window;
Controller ctrl(&model);
window.setController(&ctrl);
window.show();
return app.exec();
}