Setting dot as a seperator in LineEdit
-
I have a line edit. Im using QDoubleValidator for accepting only double values. I want to seperate my double numbers using dot.
Because i have a json file, when i use comma it gives me wrong results.How can i set this?
-
Again: Use a QDoubleSpinBox !
-
@suslucoder said in Setting dot as a seperator in LineEdit:
Because i have a json file, when i use comma it gives me wrong results.
Then do it properly when storing to JSON instead of forcing user to use dot!
double d = lineEdit->text().toDouble();
And store that d in JSON.
-
@Christian-Ehrlicher
But, Im using line edit and i will search a solution for it. -
@suslucoder said in Setting dot as a seperator in LineEdit:
But, Im using line edit and i will search a solution for it.
I really don't understand why you're trying to reinvent the wheel. Esp. when there is so few knowledge ... :/
-
@suslucoder
Best is to use aQDoubleSpinbox
, then there are no conversion issues.However, if you are determined to use a
QLineEdit
. Do not just copy the string out of that to the JSON file. As @jsulm shows you should convert it first to a C++double
, and that is what should be saved to JSON. So if the user types a comma you may or may not convert that to a double given your language locale, as you please, but it's not what will end of in the file when Qt saves adouble
to JSON. -
@JonB There is no difference between a QDoubleSpinBox and a QLineEdit except you have to not fiddle around with a regexp and you get a proper double value instead a QString.
-
@Christian-Ehrlicher
? Yes there is:QDoubleSpinbox
has adouble value() const
, whichQLineEdit
does not, and I think that might be very useful for the OP!
Ah yes, you've noted that, so I don't follow what your point is.If you mean: should he use a
QDoubleSpinbox
instead, then yes, in another thread elsewhere that the OP asked about this I was the first to say he should use a spin box! And I said that again here. I just tried to answer given that he states he wants to use aQLineEdit
instead. -
@JonB Thank you for your answer. I know using double spin box may be more efficient. But i started to do it with line edit and i wanted to know how can i solve this problem when i use it.
Now, im trying to do it with double spin box as most people suggest to me. But it seperate numbers with comma, can i use dot instead of comma? Is there any property for it?
-
@suslucoder
We have tried to explain to you that you are looking at the wrong thing. It doesn't matter what you type/how it displays in aQLineEdit
. So long as you do not just store/pass theQLineEdit::text()
to the JSON. That is the wrong thing to do for a numeric. You need to get thedouble
value of whatever the user types there --- you can allow dots or commas as you please, depending on what code you call to convert it to a number --- and pass thatdouble
value. then JSON will see a numeric and output it with a dot, regardless of your locale, which is what is needed in JSON.Can't say this any more times.
-
@JonB I understood what you are saying. I have double spin box on my ui. It seperate with comma. Like 5,000. But i want to seperate it dot. Or it should accept comma and dot both.
And i cannot gives a number like 630,12. Or i want to give 2500 but it does not allow. -
@suslucoder
I'm sorry but I (and others) have tried to explain what you need and what to do so many times, I cannot find another way of putting it. -
If you can enter a point or comma depends on your locale. If you need another locale for your QDoubleSpinBox (which is very confusing for the user) use QWidget::setLocale()
-
Just as @Christian-Ehrlicher said you need to set the proper locale to use a dot instead of a comma. However, there is no built-in way to allow for both comman and dot at the same time. You can try to have your own class derived from
QDoubleValidator
which replaces commas with a point invalidate()
.I have to disagree that
QDoubleSpinbox
is superior toQLineEdit
. I work on a scientific software. For historic reasons double input is rarely a spin box. But, it is not so easy to replace it with a spin box. If both 1000 as well as 1e-5 is a valid input there is no specific step size you can set for a spin box that makes sense. Most of the time a line edit with a validator (we also want to support both comma and point) is sufficient for the job. Yes, you do have to convert the text to a double explicitly.If you use the same thing repeatedly you should write your own little class deriving from either
QLineEdit
orQDoubleSpinbox
. For the line edit you could then implementdouble value() const
method. And if there is no step size for your spin box that makes sense, then IMHO it makes more sense to derive fromQLineEdit
.