PDA

View Full Version : QLineEdit with money format



haldrik
10th September 2008, 01:24
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?

spirit
10th September 2008, 07:16
maybe a better decision this is, use QSpinBox ? :)

aamer4yu
10th September 2008, 18:26
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.

haldrik
10th September 2008, 21:02
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.

wysota
10th September 2008, 21:49
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.

haldrik
11th September 2008, 15:10
Ok I'm working on QSpinBox using prefix("$"), but don't know how get dots separators.

wysota
11th September 2008, 16:21
Reimplement textFromValue() and textToValue() methods.

ocean
11th September 2008, 20:44
I needed the same thing in my program. Here is the code that i think help you to figure this out. By using
Util::addToRd("57836", QChar('.'), 3) you can convert 57836 to 57.836


QString Util::reverse(QString what)
{
int len = what.length();
QString buffer;
int i;
for(i=0; i<len ; i++)
buffer[i]=what[len-i-1];
return buffer;
}

QString Util::addToRd(QString what, QChar add, int kdabir)
{
QString buffer = reverse(what);
int len = what.length();
int sira;
int deviation=0;
for(sira=kdabir; sira<len ;sira=(sira+(1*kdabir)) )
{
buffer.insert(sira+deviation, add);
deviation++;
}
return reverse(buffer);
}

wysota
11th September 2008, 20:49
Hmm... how about simply:

QString text = "12345";
QString decIncluded = QString::number(text.toDouble()/1000.0, 'f', 3);

ocean
11th September 2008, 21:09
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 :D -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.

jpn
12th September 2008, 16:34
Group separators in numbers are locale specific. QLocale would handle those for you.