Noob problem with QLCDnumber
-
I'm trying to create a calculator app to learn more about using Qt. I've created the UI no problem, however, I'm trying to make it display the numbers when the button is pressed, but it keeps crashing every time I press a button that I've added a socket for. I ran the debugger and it said that there was an exception. I can't tell what the exception is though.
Display is the QLCDNumber thingy.
@
#include "calculator.h"
#include <QLCDNumber>
#include <QMessageBox>
Calculator::Calculator(QWidget *parent) :
QWidget(parent), ui(new Ui::Calculator)
{
ans = 0;
currVal = 0;
setupUi(this);}
long long int Calculator::ans = 0;
long long int Calculator::currVal = 0;QString Calculator::getNewVal(long long int nextDig)
{long long int val = currVal; if(nextDig==0) { if(val > 0) { QString str = QString::number(val); str.append("0"); currVal = str.toLongLong((bool*)true); return str; } else { return "0"; } } else if(nextDig==1) { QString str = QString::number(val); str.append("1"); currVal = (long long int)str.toLongLong(); return str; } return "";
}
void Calculator::on_Zero_clicked()
{
QMessageBox::information(this, "", "You have pressed Zero.");//------------------------------------------------
ui->Display->display(getNewVal(0));//problem line
//------------------------------------------------
}void Calculator::on_One_clicked()
{
QMessageBox::information(this,"", "You have pressed One.");//------------------------------------------------
ui->Display->display(getNewVal(1));//problem line
//------------------------------------------------
}@
-
Hi!
Don't do this:
@currVal = str.toLongLong((bool*)true);@
give it a variable or leave empty
@currVal = str.toLongLong();@ -
That isn't where the problem is. I'm getting an exception in the
@
ui->Display->display(getNewVal(0 or 1));
@line. I can't figure out what I'm doing wrong.
-
Did you try change your code like i said (it should help), and it's not help?
Then post your project, we will try help. -
No, that didn't help. I'll post my header file and my .cpp file.
@
#include "calculator.h"
#include <QLCDNumber>
#include <QMessageBox>
#include <QString>
Calculator::Calculator(QWidget *parent) :
QWidget(parent), ui(new Ui::Calculator)
{
ans = 0;
currVal = 0;
val1 = 0;
val2 = 0;
zero = true;
plus = false;
minus = false;
times = false;
divide = false;
toThe = false;
sqrt = false;
ln = false;
setupUi(this);}
QString Calculator::newVal(quint64 nextDig)
{quint64 val = currVal; QString str = QString::number(val); if(nextDig==0) { if(zero==false) { str.append("0"); currVal = (quint64)str.toLongLong(); // message("You have returned: " + str); return str; } else { // message("You have returned: " + str); return "0"; } } else if(nextDig==1) { if(zero==true) { str="1"; zero = false; } else str.append("1"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 2) { if(zero==true) { str="2"; zero = false; } else str.append("2"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 3) { if(zero==true) { str="3"; zero = false; } else str.append("3"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 4) { if(zero==true) { str="4"; zero = false; } else str.append("4"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 5) { if(zero==true) { str="5"; zero = false; } else str.append("5"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 6) { if(zero==true) { str="6"; zero = false; } else str.append("6"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig ==7) { if(zero==true) { str="7"; zero = false; } else str.append("7"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig ==8) { if(zero==true) { str="8"; zero = false; } else str.append("8"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } else if(nextDig == 9) { if(zero==true) { str="9"; zero = false; } else str.append("9"); currVal = (quint64)str.toLongLong(); //message("You have returned: " + str); return str; } return str;
}
void Calculator::setVal1()
{
val1 = currVal;
currVal = 0;
ui->lcdNumber->display(QString::number(currVal));
}void Calculator::setVal2()
{
val2 = currVal;
currVal = 0;
}void Calculator::message(QString message, QString title)
{
QMessageBox::information(this, title, message);
}void Calculator::on_Zero_clicked()
{
//QMessageBox::information(this, "", "You have pressed Zero.");
this->ui->lcdNumber->display(newVal(0));
}void Calculator::on_One_clicked()
{
//QMessageBox::information(this,"", "You have pressed One.");
ui->lcdNumber->display(newVal(1));
}void Calculator::on_Two_clicked()
{
ui->lcdNumber->display(newVal(2));
}void Calculator::on_Three_clicked()
{
ui->lcdNumber->display(newVal(3));
}void Calculator::on_Four_clicked()
{
ui->lcdNumber->display(newVal(4));
}void Calculator::on_Five_clicked()
{
ui->lcdNumber->display(newVal(5));
}void Calculator::on_Six_clicked()
{
ui->lcdNumber->display(newVal(6));
}void Calculator::on_Seven_clicked()
{
ui->lcdNumber->display(newVal(7));
}void Calculator::on_Eight_clicked()
{
ui->lcdNumber->display(newVal(8));
}void Calculator::on_Nine_clicked()
{
ui->lcdNumber->display(newVal(9));
}void Calculator::on_Plus_clicked()
{
setVal1();
plus = true;
}void Calculator::on_Equals_clicked()
{
if(plus)
{
setVal2();
currVal = val1 + val2;
ui->lcdNumber->display(QString::number(currVal));
}
}
@And here's the header file:
@
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "ui_calculator.h"class Calculator : public QWidget, private Ui::Calculator
{Q_OBJECT
public:
explicit Calculator(QWidget *parent = 0);private slots:
void on_Zero_clicked();
void on_One_clicked();
void on_Two_clicked();void on_Three_clicked(); void on_Six_clicked(); void on_Five_clicked(); void on_Four_clicked(); void on_Seven_clicked(); void on_Eight_clicked(); void on_Nine_clicked(); void on_Plus_clicked(); void on_Equals_clicked();
private:
Ui::Calculator *ui;
QString newVal(quint64);
void message(QString message, QString title = "");
void setVal1();
void setVal2();quint64 ans; quint64 currVal; quint64 val1; quint64 val2; bool zero; bool plus; bool minus; bool times; bool divide; bool toThe; bool sqrt; bool ln;
};
#endif
// CALCULATOR_H
@