[Beginner]Calculate two values from lineEdit



  • Hi, I am new to programming and trying to learn new things every day.
    I am trying to build a program in Qt to calculate some things. I have made severel input boxes (lineEdit) and those values I want to calculate and display in new labels.

    This is the code I have now. I think the second line is wrong. I want to do the input (a number) of lineEdit * the input of lineEdit_2. I think I need to transform the boxes from strings to numbers or something. I hope you can help me further. Thanks.

    @
    int newvalue;
    newvalue = lineEdit.text * lineEdit_2.text;
    ui->label_2->setText(newvalue);
    @



  • First you need use text() not text :)
    @lineEdit.text()@
    that gives you a QString value which you need to convert to int using toInt(). Then you do your calculations with int values end call setText(QString("%1").arg(newvalue));



  • [quote author="Anticross" date="1326276325"]First you need use text() not text :) @lineEdit.text()@ that gives you a QString value which you need to convert to int using toInt(). Then you do your calculations with int values end call setText(QString("%1").arg(newvalue));[/quote]
    Thanks. I have added the () and did some toInt() conversion.
    Now I have:
    @
    int number1;
    number1 = ui->lineEdit->text().toInt();
    int number2;
    number2 = ui->lineEdit_2->text().toInt();
    int newvalue;
    newvalue = number1 * number2;
    ui->label_7->setText(""+newvalue);
    @
    I guess this is a bit better. But still not good. I do not understand the QString("%1").arg stuff yet so I tried it this way which sounded logically after reading tutorials. But it is not, I guess;)



  • If you change the last line to something like this, it should work:
    @
    ui->label_7->setText(QString::number(newValue));
    @
    or
    @
    ui->label_7->setText(QString("%1").arg(newValue));
    @



  • Because you have very basic knowledge about Qt, read this book:
    http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip
    This is an excellent "how-to-start-with-qt" book.
    This book is free, and this is THE official Qt book.
    (The 2nd edition of this book is not free, you can buy it.)




Log in to reply
 

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