PDA

View Full Version : Weird Symbols when using toInt()



020394
14th October 2013, 17:21
Hello Guys,
I been facing a problem lately with toInt() conversion, I get wierd symbols in the label i link the toInt() to . I get "@!" and sometimes the whole string of event handler comes out for eg "void Calculator::on_Num7_clicked()"
The following are my codings
.h


#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QWidget>

#include <QDebug>
#include <QtCore>
#include<QtGui>

namespace Ui {
class Calculator;
}

class Calculator : public QWidget
{
Q_OBJECT

public:
explicit Calculator(QWidget *parent = 0);
~Calculator();

private slots:
void on_Num1_clicked();

void on_Num2_clicked();


void on_Add_clicked();



private:
Ui::Calculator *ui;
};

#endif // CALCULATOR_H

.cpp

#include "calculator.h"
#include "ui_calculator.h"
int num1=0;
QString str;



Calculator::Calculator(QWidget *parent) :
QWidget(parent),
ui(new Ui::Calculator)
{
ui->setupUi(this);
ui->lineEdit->setText("");

}

Calculator::~Calculator()
{
delete ui;
}

void Calculator::on_Num2_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"2");
}


void Calculator::on_Add_clicked()
{

str=ui->lineEdit->text();
num1=str.toInt();
ui->label->setText(""+num1);
qDebug()<<num1;

}

The qDebug() works perfectly fine , it is able to show the numbers
Thank you in advance :)

stampede
14th October 2013, 19:11
You add integer to string constant in line 33. I don't think this is what you wanted.
Use QString class methods to convert numbers to strings (QString::number, ::arg, ::setNum)