I'm trying to make a simple working example of MVC, but i miss the part of connecting signal-slots from external classes, in this case it should be implemented in the controller.PLEASE, HELP me to make this working and let me know whether this is the right approach:
The Controller
#include "controller.h"
#include <mainwindow.h>
#include <modelpartner.h>
Controller::Controller()
{
QObject::connect(&MainWindow,
SIGNAL(addItem
(int)),
&ModelPartner, SLOT(add()));
}
#include "controller.h"
#include <mainwindow.h>
#include <modelpartner.h>
Controller::Controller()
{
QObject::connect(&MainWindow, SIGNAL(addItem(int)),
&ModelPartner, SLOT(add()));
}
To copy to clipboard, switch view to plain text mode
The View
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
//add item
emit addItem(1);
}
void MainWindow::on_pushButton_clicked()
{
//remove item
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
//add item
emit addItem(1);
}
void MainWindow::on_pushButton_clicked()
{
//remove item
}
To copy to clipboard, switch view to plain text mode
The Model
#include "modelpartner.h"
#include <QSqlQueryModel>
#include <QStringList>
#include <QDebug>
ModelPartner::ModelPartner()
{
}
theList << "hello";
return &theList;
}
void ModelPartner::add(){
qDebug() << "added";
}
#include "modelpartner.h"
#include <QSqlQueryModel>
#include <QStringList>
#include <QDebug>
ModelPartner::ModelPartner()
{
}
QStringList *ModelPartner::partners(){
QStringList theList;
theList << "hello";
return &theList;
}
void ModelPartner::add(){
qDebug() << "added";
}
To copy to clipboard, switch view to plain text mode
Bookmarks