PDA

View Full Version : QLineEdit to Int



kais khelifa
25th November 2011, 21:39
Hello,

i have a QLineEdit widget (ui->window.numberOfElements) that contains the value 0.
How to increment that QLineEdit value by one? i means get the value from the QLineEdit and do a +1 an inserted again in the QLineEdit.

My problem isn't about reading or inserting in the QLineEdit but about returning the value with .text() and casting it to "Int" (number) so i can increment +1.

I tried to cast the return value to int ui->window.numberOfElements.toInt()+1, and (int) ui->window.numberOfElements + 1 but that didn't work.

the first one didn't work ssince the value reamins 0 and for the second one i get "invalid cast from type QString to type int"

Does any one have an indea on how to do this?
THanks.
K|Z

Zlatomir
25th November 2011, 21:51
Try it like:

ui->window.numberOfElements->setText(QString::number(ui->window.numberOfElements->text().toInt() +1) );

cincirin
26th November 2011, 08:54
Why you don't use QSpinBox (http://doc.qt.nokia.com/latest/qspinbox.html) which is more similar to what you want ?

kais khelifa
27th November 2011, 19:36
thanks Zlatomir that really helped !!
i took ur advice cincirin QSpinBox is better than QLineEdit THANKS.

K|Z