Error with QObject::connect(). Application Crashes. No build Error

I am trying to make a calculator app to do simple calculations. It does not show any error in Build but crashes on Run –
The program has unexpectedly finished.Desktop_Qt_5_4_2_MinGW_32bit2-Debugdebugcalc.exe crashed
calculator.h



#ifndef CALCULATOR
#define CALCULATOR
#include <QWidget>
class calculator : public QObject
{
    Q_OBJECT

public:
    calculator ( QWidget *parent=0);
    int value=0;

private slots:
    void setValue(int);

};

#endif // CALCULATOR

calculator.cpp
<pre><code>
#include <QWidget>
#include <calculator.h>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>

calculator::calculator(QWidget *parent)
{
    QWidget *window = new QWidget;
    QPushButton *add = new QPushButton;
    add->setText("+");
    QPushButton *sub = new QPushButton;
    sub->setText("-");
    QPushButton *mul = new QPushButton;
    mul->setText("*");
    QPushButton *div = new QPushButton;
    div->setText("/");
    QPushButton *enter = new QPushButton;
    enter->setText("enter");
    QLineEdit *disp = new QLineEdit(parent);
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(disp,0,0,0);
    layout->addWidget(add,1,0,0);
    layout->addWidget(sub,1,1,0);
    layout->addWidget(mul,1,2,0);
    layout->addWidget(div,1,3,0);
    layout->addWidget(enter,2,0,0);
    window->setLayout(layout);
    window->show();

    calculator field1, field2, result;


connect(add,SIGNAL(clicked()),&field1,SLOT((calculator::setValue(disp->text().toInt()))));
    disp->clear();
    connect(enter,SIGNAL(clicked()),&field2,SLOT(calculator::setValue(disp->text().toInt())));
    result.value=field1.value+field2.value;
    disp->setText(QString::number(result.value));

 }


 void calculator::setValue(int val)
{
    value=val;

}

main.cpp



    #include 
    #include 

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        calculator cal;

        return app.exec();
    }


Source: c++

Leave a Reply