PDA

View Full Version : Setting the text of a QLineEdit



td
11th September 2008, 10:28
Hi everyone,

Real n00b question here... I'm developing a simple calculator (just for addition). Have 2 QLineEdits for entering two numbers, a QPushButton to initiate the addition and a third QLineEdit to display the answer.

Now everything is going grand, all the connects are good etc but I can't set the answer to display in the third QLineEdit.

Here's the pseudo code



// firstNum and secondNum are the two input QLineEdits
void addition::add()
{
int num1, num2, r;

num1 = firstNum->text().toInt();
num2 = secondNum->text().toInt();

r = num1 + num2;

result = new QString(r);


answer->setText(result);
}



Now this tells me "error C2664: 'QLineEdit::setText' : cannot convert parameter 1 from 'QString *' to 'const QString &'" so I change the line to

answer->setText(*result)

which compiles but nothing will display in the answer section. I'm at wits end here, seems like I've tried everything! I've gotten other text in there (just debugging) but how do I get the result?

Cheers, any help appreciated.

wysota
11th September 2008, 11:01
Create the string on the stack and not on heap.

QString result = QString::number(num1+num2); // instead of new ...
answer->setText(result);

td
11th September 2008, 11:03
Thanks very much. Argh, why didn't I see that?! Just starting out with it as you may have guessed :)