Results 1 to 4 of 4

Thread: QLineEdit to Int

  1. #1
    Join Date
    Nov 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLineEdit to Int

    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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit to Int

    Try it like:
    Qt Code:
    1. ui->window.numberOfElements->setText(QString::number(ui->window.numberOfElements->text().toInt() +1) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLineEdit to Int

    Why you don't use QSpinBox which is more similar to what you want ?

  4. #4
    Join Date
    Nov 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit to Int

    thanks Zlatomir that really helped !!
    i took ur advice cincirin QSpinBox is better than QLineEdit THANKS.

    K|Z

Similar Threads

  1. Replies: 1
    Last Post: 12th October 2010, 22:20
  2. QLineEdit
    By bismitapadhy in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2009, 08:24
  3. QLineEdit
    By GuL in forum Newbie
    Replies: 3
    Last Post: 19th August 2008, 17:54
  4. QLineEdit
    By sonuani in forum Qt Tools
    Replies: 1
    Last Post: 21st February 2008, 07:52
  5. QLineEdit
    By sonuani in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2008, 11:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.