Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] The program has unexpectedly finished. Exited with code 0
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] The program has unexpectedly finished. Exited with code 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Renn
    wrote on last edited by Renn
    #1

    I want to ask. I try correction code below:

    MVC3.pro
    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = MVC3
    TEMPLATE = app

    SOURCES += main.cpp
    view.cpp
    model.cpp
    controller.cpp

    HEADERS += view.h
    model.h
    controller.h

    FORMS += 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_OBJECT

    public:
    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."

    1 Reply Last reply
    0
    • jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 2 Replies Last reply
      0
      • jsulmJ jsulm

        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
        }
        
        R Offline
        R Offline
        Renn
        wrote on last edited by
        #3

        @jsulm So what should I improve on my code?

        1 Reply Last reply
        0
        • jsulmJ jsulm

          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
          }
          
          R Offline
          R Offline
          Renn
          wrote on last edited by
          #4

          @jsulm However, if for example I turn off this command controller->Onthereceivedata(this); can walk, just to function in button only.

          jsulmJ 1 Reply Last reply
          0
          • R Renn

            @jsulm However, if for example I turn off this command controller->Onthereceivedata(this); can walk, just to function in button only.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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)).

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            0
            • jsulmJ jsulm

              @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)).

              R Offline
              R Offline
              Renn
              wrote on last edited by
              #6

              @jsulm I do not know what you mean.

              1 Reply Last reply
              0
              • jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7
                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();
                }
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 1 Reply Last reply
                1
                • jsulmJ jsulm
                  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();
                  }
                  
                  R Offline
                  R Offline
                  Renn
                  wrote on last edited by
                  #8

                  @jsulm Thank you very much for helping me.

                  1 Reply Last reply
                  0
                  • jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    No problem

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved