Important: Please read the Qt Code of Conduct -

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!

  • Welcome to forum!

    Where exactly are you stopped?

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

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

  • okay... Have you tried anything at all ?

    You can start "here": and "there":

  • 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.

  • 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..

  • 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.

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

    Hope that helps.

  • But i don't know how to make functions, i mean in c++ is a lot more easy..
    int Sum(int a, int b){
    return a+b;
    now i'm felling like i'm lost.. i thought Designer was a lot more easier..

    By default Qt made this:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    delete ui;

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

  • KK guys i've made it thanks!
    Instead of "->" i've used "."
    Thanks =)

  • [quote author="Vesco" date="1337023716"]

    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)
    void MainWindow::on_btnAdd_clicked()
    int a = ui->lineEditOne->text().toInt();
    int b = ui->lineEditTwo->text().toInt();
    int result = a+b;


    Try this if it works. As a beginner you can start with some "Video tutorials ":

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

  • 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.

  • Ok, thanks you all!

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

    Happy Coding :)

Log in to reply