Results 1 to 11 of 11

Thread: QLineEdit with money format

  1. #1
    Join Date
    Aug 2007
    Location
    Fresno - Colombia
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Cool QLineEdit with money format

    Hi.

    How I can get a QLineEdit with money format?, something like this:

    A user type 2500 and lineEdit change the view value to $2.500, then the lineEdit keep the numeric value to use on a database, converting this value to integer.

    The value $2.500 is only a view for the lineEdit, but the real value must be 2500.

    I did understand?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLineEdit with money format

    maybe a better decision this is, use QSpinBox ?

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit with money format

    I guess for line edit, you will need to store the value in a variable, and show contents based on that in the view.
    For that you may need to catch editingFinished() or returnPressed() signal of the lineedit, get the text 2500, store it in a variable. Convert it to $2.500 format, and use QLineEdit::setText to set the text in line edit.

    Also if you are using some View(inherited from QAbstractItemView), you can use delegates to show the text in desired format.

  4. #4
    Join Date
    Aug 2007
    Location
    Fresno - Colombia
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit with money format

    Quote Originally Posted by aamer4yu View Post
    I guess for line edit, you will need to store the value in a variable, and show contents based on that in the view.
    For that you may need to catch editingFinished() or returnPressed() signal of the lineedit, get the text 2500, store it in a variable. Convert it to $2.500 format, and use QLineEdit::setText to set the text in line edit.
    I think this approach don't like to me.
    Also if you are using some View(inherited from QAbstractItemView), you can use delegates to show the text in desired format.
    This is interesting, but I'm using only QLineEdit. I'll take a view.

  5. #5
    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: QLineEdit with money format

    I second the idea of using a spin box instead of a line edit. But if you really want a pure line edit, implement a validator that would introduce the missing symbols into the text. Unfortunately you'll have to strip those symbols from the text before writing it to the database, so a spin box is a better solution.

  6. #6
    Join Date
    Aug 2007
    Location
    Fresno - Colombia
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit with money format

    Ok I'm working on QSpinBox using prefix("$"), but don't know how get dots separators.

  7. #7
    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: QLineEdit with money format

    Reimplement textFromValue() and textToValue() methods.

  8. #8
    Join Date
    Sep 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit with money format

    I needed the same thing in my program. Here is the code that i think help you to figure this out. By using
    Qt Code:
    1. Util::addToRd("57836", QChar('.'), 3)
    To copy to clipboard, switch view to plain text mode 
    you can convert 57836 to 57.836
    Qt Code:
    1. QString Util::reverse(QString what)
    2. {
    3. int len = what.length();
    4. QString buffer;
    5. int i;
    6. for(i=0; i<len ; i++)
    7. buffer[i]=what[len-i-1];
    8. return buffer;
    9. }
    10.  
    11. QString Util::addToRd(QString what, QChar add, int kdabir)
    12. {
    13. QString buffer = reverse(what);
    14. int len = what.length();
    15. int sira;
    16. int deviation=0;
    17. for(sira=kdabir; sira<len ;sira=(sira+(1*kdabir)) )
    18. {
    19. buffer.insert(sira+deviation, add);
    20. deviation++;
    21. }
    22. return reverse(buffer);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ocean; 11th September 2008 at 21:53.

  9. #9
    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: QLineEdit with money format

    Hmm... how about simply:
    Qt Code:
    1. QString text = "12345";
    2. QString decIncluded = QString::number(text.toDouble()/1000.0, 'f', 3);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Sep 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit with money format

    Yes, your solution works perfect! (and simpler). But if you want to handle much bigger numbers, you need to use more flexible code. Because your code won't render two or more DOTs and also you won't be able to use any type of decimal numbers. (I use this code to convert i.e. 12536.89 to 12.536,89). To be honest, i think i will never have to enter 1.000.000$ into my LineEdit -and i don't think you will need that in near future too, am i right?-, but sometimes it's good to know that you have a function that can handle any type of argument, in this situation, big numbers.
    Last edited by ocean; 11th September 2008 at 22:01.

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit with money format

    Group separators in numbers are locale specific. QLocale would handle those for you.
    J-P Nurmi

Similar Threads

  1. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  2. Replies: 2
    Last Post: 20th March 2008, 14:37
  3. Is there a known problem with QMimeData on Mac OS X?
    By Wurgl in forum Qt Programming
    Replies: 8
    Last Post: 27th February 2008, 22:21
  4. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  5. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

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.