You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Fily

Beginner

  • "Fily" is male
  • "Fily" started this thread

Posts: 24

Location: USA

  • Send private message

1

Wednesday, April 4th 2012, 3:30am

Displaying a simple calculation in Qt

Hi,
I have been playing around with Qt and Qt Creator but I’m having a hard time to figuring out something incredibly easy, what I need to do is basically get two inputs (numbers) from the user, do some calculations and then display the result in a third text field when a button is clicked.
This is the C++ code

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
int num1;
int num2;
int total;

cout << "First number" << endl;
cin >> num1;
cout << "Second number" << endl;
cin >> num2;

//calculate
total = num1 * num2;
//display result
cout << total << endl;



Can someone help me to create what I have described in the above code?
I know, I know its something very simple but I cannot make it work using textEdits and plainTextEdits.

I tried ….

Source code

1
2
3
4
5
6
7
8
9
voidMainWindow::on_pushButton_clicked()[*]
{
 int num1 = 5;
 int num2 = 10;
 int total; 
 total = num1 + num2;

 ui->textEdit->setPlainText(total);[*]
}

… but nothing.
Thanks a lot for your help!

2

Wednesday, April 4th 2012, 10:42am

Source code

1
2
3
4
5
6
7
8
9
10
voidMainWindow::on_pushButton_clicked()[*]
{
 int num1 = 5;
 int num2 = 10;
 int total; 
 total = num1 + num2;

 QString numAsString = QString::number(total);
 ui->textEdit->setPlainText(numAsString); //[*]
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Fily

Beginner

  • "Fily" is male
  • "Fily" started this thread

Posts: 24

Location: USA

  • Send private message

3

Wednesday, April 4th 2012, 1:21pm

This is my final code. Thanks a LOT for your help


Source code

1
2
3
4
5
6
7
8
9
void MainWindow::on_pushButton_clicked()
{
 QString num1 = ui->lineEdit->text();
 QString num2 = ui->lineEdit_2->text();

 int total = num1.toInt()+ num2.toInt();

 ui->plainTextEdit->setPlainText(QString::number(total, 10));
}

This post has been edited 1 times, last edit by "Fily" (Apr 4th 2012, 1:27pm)