Unsolved [SOLVED] The program has unexpectedly finished. Exited with code 0
-
I want to ask. I try correction code below:
MVC3.pro
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MVC3
TEMPLATE = appSOURCES += main.cpp
view.cpp
model.cpp
controller.cppHEADERS += view.h
model.h
controller.hFORMS += view.ui
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "model.h"
#include "view.h"
#include <QDebug>class Model;
class View;class Controller
{
public:
Controller(Model* m);
~Controller();void OnreceiveData(View* v);
private:
Model* model;
};#endif // CONTROLLER_H
model.h
#ifndef MODEL_H
#define MODEL_H
#include <QtGui>
#include <QtCore>
#include "controller.h"class Controller;
class Model
{
public:
Model();
~Model();
QStringList getValueAngka();
void getValue();
QStringList hasil;private:
int r;
int e;
};#endif // MODEL_H
view.h
#ifndef VIEW_H
#define VIEW_H#include <QMainWindow>
#include "controller.h"namespace Ui {
class View;
}class Controller;
class View : public QMainWindow
{
Q_OBJECTpublic:
explicit View(QWidget parent = 0);
~View();
void setController(Controller c);QString getAngka1(); QString getAngka2(); void setHasil(QStringList n);
public slots:
void receiveData();private:
Ui::View *ui;Controller* controller; QString angka1; QString angka2;
};
#endif // VIEW_H
controller.cpp
#include "controller.h"
#include "model.h"
#include "view.h"
#include <QDebug>Controller::Controller(Model *m):model(m){}
Controller::~Controller(){}void Controller::OnreceiveData(View *v)
{
//QString angka1 = v->getAngka1();
//QString angka2 = v->getAngka2();
model->getValue();
//model->getValue();
//model->getValue(angka1.toInt(), angka2.toInt());
v->setHasil(model->getValueAngka());
}main.cpp
#include "view.h"
#include "model.h"
#include "controller.h"#include <QApplication>
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();
}
model.cpp
#include "model.h"Model::Model() {}
Model::~Model(){}void Model::getValue()
{
hasil << "1";
// r = 1;
// e = 3;
// hasil = r + e;
}QStringList Model::getValueAngka()
{
return hasil;
}view.cpp
#include "view.h"
#include "ui_view.h"
#include "controller.h"
#include <QDebug>void View::setController(Controller *c)
{
controller = c;
}View::View(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::View)
{
ui->setupUi(this);
controller->OnreceiveData(this);
ui->input1->setPlaceholderText("Masukkan input pertama");
ui->input2->setPlaceholderText("Masukkan input kedua");
connect(ui->hitungBtn, SIGNAL(clicked()), this, SLOT(receiveData())); // Mengirim value ke Controller melalui Button
}View::~View()
{
delete ui;
}QString View::getAngka1()
{
return ui->input1->text();
}QString View::getAngka2()
{
return ui->input2->text();
}void View::setHasil(QStringList n)
{
ui->hasiils->setText(n.at(0));
qDebug() << n;
}void View::receiveData()
{
controller->OnreceiveData(this);
}Is there something wrong with my code until the notification "The program has unexpectedly finished."
-
Here you do not check whether the list is empty or not:
void View::setHasil(QStringList n) { ui->hasiils->setText(n.at(0)); qDebug() << n; }
Another issue is - controller is not initialized when you call it in the constructor:
void View::setController(Controller *c) { controller = c; // Initialization here } View::View(QWidget *parent) : QMainWindow(parent), ui(new Ui::View) { ui->setupUi(this); controller->OnreceiveData(this); // Here you dereference uninitialized pointer! ui->input1->setPlaceholderText("Masukkan input pertama"); ui->input2->setPlaceholderText("Masukkan input kedua"); connect(ui->hitungBtn, SIGNAL(clicked()), this, SLOT(receiveData())); // Mengirim value ke Controller melalui Button }
-
@jsulm So what should I improve on my code?
-
@jsulm However, if for example I turn off this command controller->Onthereceivedata(this); can walk, just to function in button only.
-
@Renn Well, I already told you what you should improve:
void View::setHasil(QStringList n) { if (n.size() > 0) // Do not call n.at(0) if n is empty! ui->hasiils->setText(n.at(0)); qDebug() << n; }
Either do not call controller->OnreceiveData(this); in View::View(QWidget *parent) or make sure controller is already initialized (you can pass pointer to controller to your View constructor, then you do not need View::setController(Controller *c)).
-
@jsulm I do not know what you mean.
-
View::View(Controller *controller, QWidget *parent) : QMainWindow(parent), ui(new Ui::View) { ui->setupUi(this); this->controller = controller; this->controller->OnreceiveData(this); ui->input1->setPlaceholderText("Masukkan input pertama"); ui->input2->setPlaceholderText("Masukkan input kedua"); connect(ui->hitungBtn, SIGNAL(clicked()), this, SLOT(receiveData())); // Mengirim value ke Controller melalui Button } int main(int argc, char *argv[]) { QApplication app(argc, argv); Model model; Controller ctrl(&model); View window(&ctrl); window.show(); return app.exec(); }
-
@jsulm Thank you very much for helping me.
-
No problem