How to connect the controller with the view and vice versa?

Viewed 99

I'm developing a little app in C++ using the MVP pattern, but I'm not able to connect the controller with the view and vice versa to exchange signals and slots.

This is the overview of my application: enter image description here

In particular, from the menu bar, when I choose a QAction, a window opens with fields to fill in. Once you have filled in the fields of the window and pressed the "Add service" button, I should be able to send the signal to the controller (by means of a triggered() signal) which will have to notify the model (to add a new service in the container). This model, as soon as it has added the service in the container, will have to emit a signal (serviceAdded() that the container will have to "capture" and then do other things (update the view with the services present in the container and close the open window to add service). I wanted to understand how to connect the controller and the view when: the QPushButton is pressed to add the service and how to close the window, after receiving the signal from the model.

enter image description here

I set up the controller like this:

class Controller : public QObject{
Q_OBJECT
private:
    Model * model;
    MainView * view;
public:
    explicit Controller(Model * m, MainView * mv);

While my main view is set up like this:

class Controller;

class MainView : public QWidget {
    Q_OBJECT
private:
    QHBoxLayout* mainLayout;
    QTableWidget* table;
    QMenuBar* menu;
    Controller * controller; 
public:
    explicit MainView(QWidget *parent=nullptr);

When QAction (QAction * artGenerico) is pressed, this happens:

connect(artGenerico, SIGNAL(triggered()), this, SLOT(articoloGenericoWindow()));

This code is part of the view.

And “articoloGenericoWindow()” is:

void MainView::articoloGenericoWindow(){
ArticoloGenericoView * w = new ArticoloGenericoView();
w->show();
connect(controller, SIGNAL(closeWindow()), this, SLOT(close()));
}

Also this code is part of the view.

The problem is that the controller is not connected to anything, even the compiler tells me: “QObject:: connect: Cannot connect (nullptr):: closeWindow () to MainView:: close ()”. In fact, the window does not close when I click on the button.

How can I connect the controller with the view?

I hope I have expressed myself correctly.

1 Answers

If you also have a problem of this type, I solved it by connecting the model and the view to the controller and the view to the controller simply by creating setting methods (in the controller and in the view) and connecting everything in the main.

This is the main:

MainView w;
Controller c;
Model m;

c.setModel(&m);
c.setView(&w);
w.setController(&c);
w.show();

The setters for the controller:

void Controller::setModel(Model* m) {
    model=m;
}
void Controller::setView(MainView* mv) {
    view=mv;
}

The setter for the view:

void MainView::setController(Controller * c){
    controller=c;
    connect(...)
}

In the constructors I simply defined the QWidget:

explicit MainView(QWidget *parent=nullptr);

Implementation:

MainView::MainView(QWidget * parent) : QWidget(parent){...}

Enjoy!

Related