Results 1 to 3 of 3

Thread: check if value is int or numeric

  1. #1
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2

    Default check if value is int or numeric

    Last night I has asked for a validator to for checking if a number was an int and I got a good hand. One of my co-workers told me they're going to need a double. The code will throw an error for white space and for special characters so I'm not sure what the approach is. Thanks for the clarificatioin

    Qt Code:
    1. double GateOverlapFactor::on_okButton_clicked()
    2. {
    3. QString isNumber = m_ui->lineEdit->text();
    4. QString trueNumber = "";
    5. QString str = m_ui->lineEdit->text();
    6. double returnDouble;
    7.  
    8. foreach(QChar c, str)
    9. {
    10. if(c.isNumber()) //check numeric input
    11. {
    12. trueNumber = m_ui->lineEdit->text();
    13.  
    14. close();
    15. }
    16. if(isspace(true)) // Check for whitespace
    17. {
    18. m_ui->messageLbl->setText(m_ui->lineEdit->text() + "Enter a Numeric Value");
    19. }
    20. else // not numeric?, throw error
    21. {
    22. m_ui->messageLbl->setText(m_ui->lineEdit->text() + ": Is Invalid. Enter a Numeric Value");
    23.  
    24. }
    25. }
    26.  
    27. returnDouble = trueNumber.toDouble();
    28.  
    29. return returnDouble;
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: check if value is int or numeric

    If you want to enter only double values in line edit then all you need is:
    Qt Code:
    1. QDoubleValidator* validator = new QDoubleValidator(this);
    2. m_ui->lineEdit->setValidator(validator);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2

    Default Re: check if value is int or numeric

    Quote Originally Posted by RSX View Post
    If you want to enter only double values in line edit then all you need is:
    Qt Code:
    1. QDoubleValidator* validator = new QDoubleValidator(this);
    2. m_ui->lineEdit->setValidator(validator);
    To copy to clipboard, switch view to plain text mode 
    thank you so much, was on vacation last week

Similar Threads

  1. CHeck if tbox Value is numeric
    By Swankee in forum Newbie
    Replies: 2
    Last Post: 30th October 2009, 01:41
  2. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 20:24
  3. Replies: 0
    Last Post: 2nd May 2008, 07:57
  4. Check a point inside or outside a road?
    By kstking in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2007, 18:48
  5. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43

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.