#include "calc.h"
#include "ui_calc.h"
: QDialog(parent
), ui
(new Ui
::Calc) {
value = 0;
ui->setupUi(this);
this->setWindowTitle("Calqta");
connect(ui->firstButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->firstButton, "1");
connect(ui->secondButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->secondButton, "2");
connect(ui->thirdButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->thirdButton, "3");
connect(ui->fourthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->fourthButton, "4");
connect(ui->fifthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->fifthButton, "5");
connect(ui->sixthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->sixthButton, "6");
connect(ui->seventhButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->seventhButton, "7");
connect(ui->eighthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->eighthButton, "8");
connect(ui->ninethButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->ninethButton, "9");
connect(ui->zeroButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->zeroButton, "0");
connect(signalMapper,
SIGNAL(mapped
(const QString &)),
this,
SLOT(changeValue
(const QString &)));
connect(ui->sqrtButton, SIGNAL(clicked()), this, SLOT(square()));
connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
}
Calc::~Calc()
{
delete ui;
}
void Calc
::add(const QString &val
){
str+=val;
}
void Calc
::changeValue(const QString &val
){
Calc::add(val);
value=str.toInt();
display(value);
}
void Calc::display(const int &val){
ui->lcdNumber->display(val);
}
void Calc::square(){ //works
value = std::sqrt(value);
display(value);
str.remove(0, str.length());
Calc
::changeValue(QString::number(value
));
}
void Calc::clear(){ //works
value = 0;
str.remove(0, str.length());
display(value);
}
#include "calc.h"
#include "ui_calc.h"
Calc::Calc(QWidget *parent)
: QDialog(parent), ui(new Ui::Calc)
{
value = 0;
ui->setupUi(this);
this->setWindowTitle("Calqta");
QSignalMapper *signalMapper = new QSignalMapper();
connect(ui->firstButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->firstButton, "1");
connect(ui->secondButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->secondButton, "2");
connect(ui->thirdButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->thirdButton, "3");
connect(ui->fourthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->fourthButton, "4");
connect(ui->fifthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->fifthButton, "5");
connect(ui->sixthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->sixthButton, "6");
connect(ui->seventhButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->seventhButton, "7");
connect(ui->eighthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->eighthButton, "8");
connect(ui->ninethButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->ninethButton, "9");
connect(ui->zeroButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(ui->zeroButton, "0");
connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(changeValue(const QString &)));
connect(ui->sqrtButton, SIGNAL(clicked()), this, SLOT(square()));
connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
}
Calc::~Calc()
{
delete ui;
}
void Calc::add(const QString &val){
str+=val;
}
void Calc::changeValue(const QString &val){
Calc::add(val);
value=str.toInt();
display(value);
}
void Calc::display(const int &val){
ui->lcdNumber->display(val);
}
void Calc::square(){ //works
value = std::sqrt(value);
display(value);
str.remove(0, str.length());
Calc::changeValue(QString::number(value));
}
void Calc::clear(){ //works
value = 0;
str.remove(0, str.length());
display(value);
}
To copy to clipboard, switch view to plain text mode
I managed that simple dialog, so I came back to my calculator. All numeric buttons work correctly - adding digit to number works. Square root also works correctly. Now I have a problem with 2 arguments operations, such as addition. I don't have an idea how can I store the second argument - first is in the variable "value". Could you give me handy tip?
Bookmarks