Results 1 to 9 of 9

Thread: Enter only numbers in the text edit??

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Enter only numbers in the text edit??

    How to make the text edit to allow for number only?? It should not allow any alphabets to enter on it..

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Enter only numbers in the text edit??


  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enter only numbers in the text edit??

    More specifically QIntValidator.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Enter only numbers in the text edit??

    Quote Originally Posted by high_flyer View Post
    More specifically QIntValidator.
    I would have given him a more specialized information, but unfortunately he didn't specify what kind of numbers he wants to display.

    If he wants to display doubles, a QIntValidator would not be very useful

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enter only numbers in the text edit??

    True, but I took a guess.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Enter only numbers in the text edit??

    QIntValidator and QDoubleValidator are not good choice because they allow to input numbers that contains digits, +- signs, decimal point sign etc. You should use QRegExpValidator with regular expression [0-9]*
    P.S.
    Ooops. You write about number input not digit. QIntValidator and QDoubleValidator are good choice.
    Last edited by Lesiok; 23rd March 2011 at 16:41.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Enter only numbers in the text edit??

    How to make the text edit to allow for number only??
    QIntValidator and QDoubleValidator are not good .... numbers that contains digits, +- signs, decimal point sign etc.
    -12.234 is still a number.
    Since he didn't provide any information about other restrictions all solutions suggested so far MIGHT be correct, but not necessarily.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Enter only numbers in the text edit??

    Guys, there is no need for any validators here. One needs to listen to the contentsChange() signal from QTextDocument, extract the characters added and use QString::toInt() or other appropriate variant to see if it is of the required class. Then the changes made need to be undone (in its simplest form by calling undo) if needed. If you intend to validate 100 pages of numbers using QIntValidator then, well... have a good time
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Enter only numbers in the text edit??

    I'd subclass the text edit, call it numberEdit, and add it to my library of components. Then next time you want a similar function it's already done so you just grab the code

    (You may have guessed I hate writing the same things multiple times)

    To filter out the "alphabets", I'd probably sniff the events or similar, depending on how my research into the documentation of textedit went.

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 19:06
  2. Replies: 2
    Last Post: 17th October 2010, 12:16
  3. Replies: 3
    Last Post: 26th August 2010, 08:57
  4. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  5. Replies: 8
    Last Post: 15th May 2007, 09:21

Tags for this Thread

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.