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
    @ui->lineEdit->setText(QString::number(1234));@



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



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

    You can start "here":http://www.developer.nokia.com/Community/Wiki/How_to_use_Qt_Creator_IDE and "there":https://qt-project.org/videos



  • 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":http://qt-project.org/doc/qt-4.8/qlineedit.html#text-prop . You need to convert text values of QLineEdit s to integer. It's done by "QString::toInt":http://qt-project.org/doc/qt-4.8/qstring.html#toInt . Then just add your numbers, and put result in desired QLineEdit. Convert integer to QString by "QString::number":http://qt-project.org/doc/qt-4.8/qstring.html#number-4 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..
    like:
    @
    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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~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..

    [/quote]

    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;
    ui->lineEditThree->setText(QString::number(result));

    }@

    Try this if it works. As a beginner you can start with some "Video tutorials ":http://www.voidrealms.com/tutorials.aspx?filter=qt



  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.