Model-View-Presenter(MVP) Design Pattern in Qt Application

From Qt Wiki
Revision as of 11:58, 13 April 2016 by Satyendra (talk | contribs)
Jump to navigation Jump to search

Can we implement MVP(Model-View-Presenter) design pattern using Qt's Signal and slot mechanism.

We will try to create a Qt application with MVP design pattern.

First of all What is MVP design pattern? MVP is a user interface architectural pattern engineered to facilitate separation of logic out of the view and into the presenter.

First we refer to existing documentation about Model-View-Presenter.MVP Wiki documentation says like this. a)The Presenter is common to multiple views. b)The model is an interface defining the data to be displayed or otherwise acted upon in the user interface. c)The View is kept as thin and ignorant as possible.

In MVP the presenter assumes the functionality of the "middle-man". In MVP, all presentation logic (Signal and slots and connect statements)is pushed to the presenter class.

Remaining views only has the implementation to display it self.

I will add the code snippets for the three views and its presenter class.

I have an application with Three Views the object interaction happens via the Presenter class.

ViewOne class implementation

#include <QWidget>

namespace Ui {
class ViewOne;
}

class ViewOne : public QWidget
{
    Q_OBJECT

public:
    explicit ViewOne(QWidget *parent = 0);
    ~ViewOne();

private:
    Ui::ViewOne *ui;
};
#include "viewone.h"
#include "ui_viewone.h"

ViewOne::ViewOne(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ViewOne)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SIGNAL(ViewTwoButtonclicked()));

}

ViewOne::~ViewOne()
{
    delete ui;
}