View Full Version : QLineEdit and Float Format !!
jesse_mark
27th July 2012, 16:32
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
tescrin
27th July 2012, 19:32
You'll need to subclass it IIRC.
Off the top of my head the pseudo code looks like this:
connect(textEdit event, qlineeditsubclass::mysupercoolslot)
qlineeditsubclass::mysupercoolslot()
{
setCurrentDisplayedString(QString(get <CURR_STRING>,'f',2));
}
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.
wysota
27th July 2012, 19:47
Install a validator on the line edit that will fixup() the content according to your needs.
jesse_mark
27th July 2012, 20:53
@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
wysota
27th July 2012, 21:17
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().
tescrin
27th July 2012, 22:15
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:
#include <QLineEdit>
class MyLineEdit :public QLineEdit{
public:
//constructor/destructors go here
//something close to this. Syntax might be off a bit
void fixup ( QString & input ) const { input = input.number(input,'f',2);}
};
Then just promote in your UI if you need to. It's not scary :)
wysota
28th July 2012, 21:39
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.