Results 1 to 7 of 7

Thread: QLineEdit and Float Format !!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    173
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 2 Times in 2 Posts

    Default QLineEdit and Float Format !!

    i have a QLineEdit and i want it to display float Number so add ".00"
    automatically when ever the user enter number with out the "." or when i use "setText" so is there a feature i can use to do that, or i have to code it ??

    for example :

    if the user enter "5466" when user leave the lineEdit it make "5466.00" and the same case when if i used setText

    Thank you

  2. #2
    Join Date
    Jun 2012
    Posts
    98
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    11

    Default Re: QLineEdit and Float Format !!

    You'll need to subclass it IIRC.

    Off the top of my head the pseudo code looks like this:
    Qt Code:
    1. connect(textEdit event, qlineeditsubclass::mysupercoolslot)
    2. qlineeditsubclass::mysupercoolslot()
    3. {
    4. setCurrentDisplayedString([B]QString(get <CURR_STRING>,'f',2)[/B]);
    5. }
    To copy to clipboard, switch view to plain text mode 

    QString::number(<CURR_STRING>,'f',2)

    Where get <CURR_STRING> is just asking the QLineEdit for a copy of its string, 'f' says you want floating point format, and '2' says you want 2 decimal places.

    Works like a charm.

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

    Default Re: QLineEdit and Float Format !!

    Install a validator on the line edit that will fixup() the content according to your needs.
    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.


  4. #4
    Join Date
    Jun 2012
    Posts
    173
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 2 Times in 2 Posts

    Default Re: QLineEdit and Float Format !!

    @tescrin, thank you so much,but i really don't prefer to subclass it.

    @wayson,

    i though vaidatores are just ensure the input is limited depending on what Regexp u have , and return (valid, intermediate or invalid). but how i can make it fix the text entered to the format i want??

    could you please explain with an example or point me to where i can fine one.

    thank you soo much

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

    Default Re: QLineEdit and Float Format !!

    Quote Originally Posted by jesse_mark View Post
    i though vaidatores are just ensure the input is limited depending on what Regexp u have , and return (valid, intermediate or invalid). but how i can make it fix the text entered to the format i want??
    Both validate() and fixup() take a non-const reference to the text they process so you can actually modify it to return an accepted value. Take a look at the docs of QValidator::fixup().
    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.


  6. #6
    Join Date
    Jun 2012
    Posts
    98
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    11

    Default Re: QLineEdit and Float Format !!

    Neat; so he'd set a QRegExp that only accepts input when it has the two decimal points, and he'd reimplement fixup?

    If so, it's a better version of my answer => Jesse_mark will still need to subclass

    As a side note, I wouldn't be afraid of subclassing. You can fit all of your code into the header file probably:

    Qt Code:
    1. #include <QLineEdit>
    2.  
    3. class MyLineEdit :public QLineEdit{
    4. public:
    5. //constructor/destructors go here
    6.  
    7. //something close to this. Syntax might be off a bit
    8. void fixup ( QString & input ) const { input = input.number(input,'f',2);}
    9. };
    To copy to clipboard, switch view to plain text mode 

    Then just promote in your UI if you need to. It's not scary

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

    Default Re: QLineEdit and Float Format !!

    Quote Originally Posted by tescrin View Post
    If so, it's a better version of my answer => Jesse_mark will still need to subclass
    No, why? Using QLineEdit is perfectly fine. Only a subclass of QValidator is needed.
    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.


Similar Threads

  1. Replies: 4
    Last Post: 18th May 2011, 17:59
  2. QLineEdit automatic money format.
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2009, 07:24
  3. QLineEdit with money format
    By haldrik in forum Qt Programming
    Replies: 10
    Last Post: 12th September 2008, 16:34
  4. Replies: 2
    Last Post: 20th March 2008, 14:37
  5. QLineEdit - Float number not displayed correct
    By morty in forum Qt Programming
    Replies: 3
    Last Post: 9th November 2006, 00:47

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
  •  
Qt is a trademark of The Qt Company.