May 14, 2012

Vesco Vesco
Lab Rat
7 posts

Sum 2 integers

 

Hi all, i need to do a presentation of Qt Designer for my class.
So i’m going to do an application which does the sum between 2 integers and place the result into a TextBox.
But i’m having trouble making it.
I hope you guys can help me!

14 replies

May 14, 2012

Rahul Das Rahul Das
Robot Herder
362 posts

Welcome to forum!

Where exactly are you stopped?

Just a guess, may be at the int to QString part ? try this

  1. ui->lineEdit->setText(QString::number(1234));

 Signature 

——————————-

    Rahul Das

——————————-

May 14, 2012

Vesco Vesco
Lab Rat
7 posts

I don’t understand how to make this application in qt designer..

May 14, 2012

Rahul Das Rahul Das
Robot Herder
362 posts

okay… Have you tried anything at all ?

You can start here [developer.nokia.com] and there [qt-project.org]

 Signature 

——————————-

    Rahul Das

——————————-

May 14, 2012

Sam Sam
Area 51 Engineer
613 posts

Hi,

Kindly provide some code/images that will be more easy for us to understand where exactly u need help. But dont expect us to do your homework :)

Welcome to the forum.

May 14, 2012

Vesco Vesco
Lab Rat
7 posts

So far i did:
created a new project like this Qt Widget Project -> Qt Gui Application
Then in the Designer i placed 3 textEdit.
I know the Signals Slots functions, but i can’t make a sum between this 2 integers..

May 14, 2012

Rahul Das Rahul Das
Robot Herder
362 posts

so far, so good.

How do you do it in regular c++ ?? same way you do addition!!

and i have already given you the code to set text.

 Signature 

——————————-

    Rahul Das

——————————-

May 14, 2012

soroush soroush
Dinosaur Breeder
781 posts

Each line edit holds its text value in a QString object. You can access values by QLineEdit::text [qt-project.org] . You need to convert text values of QLineEdit s to integer. It’s done by QString::toInt [qt-project.org] . Then just add your numbers, and put result in desired QLineEdit. Convert integer to QString by QString::number [qt-project.org] and set text of QLineEdit.

Hope that helps.

May 14, 2012

Vesco Vesco
Lab Rat
7 posts

But i don’t know how to make functions, i mean in c++ is a lot more easy..
like:

  1. int Sum(int a, int b){
  2. return a+b;
  3. }

now i’m felling like i’m lost.. i thought Designer was a lot more easier..

By default Qt made this:

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13.     delete ui;
  14. }

[Edit: Please add @ tags around your code; mlong]

May 14, 2012

Vesco Vesco
Lab Rat
7 posts

KK guys i’ve made it thanks!
Instead of “->” i’ve used “.”
Thanks =)

May 14, 2012

Sam Sam
Area 51 Engineer
613 posts

Vesco wrote:

now i’m felling like i’m lost.. i thought Designer was a lot more easier..

The Qt Designer makes things far more easier when we are creating a forms and adding widgets. The more you practice the more you learn. So as you have already added three textbox/lineEdit you can further set the objectName in the property editor as lineEditOne , lineEditTwo and lineEditThree. Also you can further add button/pushButton and set the objectName as btnAdd.

So that the user can enter the values in the lineEdit and Click on Add button to display the result in lineEditThree.

You can try the following code on_btnAdd_Clicked() slot(you can create through designer)

  1. void MainWindow::on_btnAdd_clicked()
  2. {
  3.   int a = ui->lineEditOne->text().toInt();
  4.   int b = ui->lineEditTwo->text().toInt();
  5.   int result = a+b;
  6.   ui->lineEditThree->setText(QString::number(result));
  7.  
  8. }

Try this if it works. As a beginner you can start with some Video tutorials [voidrealms.com]

May 15, 2012

Vesco Vesco
Lab Rat
7 posts

Ok, it works, but I do have a question.
What’s the differences between QString::number(); and .toInt(); ?

May 15, 2012

Sam Sam
Area 51 Engineer
613 posts

The lineEdit::text() returns a QString so in order to assign it to an integer value we need to cast it, so we use .toInt(). Similarly when we are setting the value to the lineEdit it has to be a QString. So we cast it from int to QString using QString::number();

Look at Soroush’s post above.

May 15, 2012

Vesco Vesco
Lab Rat
7 posts

Ok, thanks you all!

May 15, 2012

Sam Sam
Area 51 Engineer
613 posts

Kindly Edit your first post and add [Solved] to the title.

Happy Coding :)

 
  ‹‹ Populate treeview      Show Tooltip on wrong input in QLineEdit ››

You must log in to post a reply. Not a member yet? Register here!