I have a UI with a QLineEdit which only accepts float/double values with the help of a QDoubleValidator.
It only accepts values if I enter a leading number like: 0.234. But I'd prefer to be able to enter values directly without a leading number like .234. Unfortunately the QDoubleValidator does not accept a leading point. Is there any way to archive my goal with the help of the validator, or do I have to check every entered character myself? I'm using Qt 5.9.1 on Windows10.
QDoubleValidator* doubleValidator = new QDoubleValidator();
QLineEdit* lineEdit = new QLineEdit(frame);
lineEdit->setValidator(doubleValidator);
vbox->addWidget(lineEdit);
Unfortunately, QDoubleValidator is pretty limited, but you can use QRegExpValidator to get what you want with a regex describing a number, depending on the notation you expect.
// non-scientific floating-point numbers
QRegExp rx("[-+]?[0-9]*\\.?[0-9]+");
QRegExpValidator v(rx, 0);
QString s;
s = ".123";
v.validate(s, 0); // Returns Acceptable
This is much more extendable, and allows you to abstract it to any condition, with basic regular expression knowledge.
Related
I want the QLineEdit to accept only numbers without any decimal.e.g it should accept '456' but not '456.3434'.i.e. it should not allow decimal at all. Can anybody give some pointers that how can i do that.
I tried to use QIntValidator, but it still allows to enter decimal point, and when i convert the text from QLinEdit it returns zero (as it documentation says, if the conversion fails it will return zero).
I also tried to use QRegExpValidator( QRegExp("[0-9]"), but it allows only one number. There is no limit for the maximum number, how do i specify the QRegExp with minimum as 0 and maximum as undefined, if QRegExpValidator is the only way to achieve it ?
Thank you
You might try the following validator:
QLineEdit le;
le.setValidator(new QRegExpValidator(QRegExp("[0-9]+")));
le.show();
UPDATE
To allow input in exponential form you can try this:
le.setValidator(new QRegExpValidator( QRegExp("[0-9]+e[0-9]+")));
I'm using a QDoubleValidator for my QLineEdit. The application locale (set in QtCreator) is QLocale::German.
Now when I enter a valid double (either using a dot or a comma as decimal separator) writing to the textedit as well as converting the string to a float works perfectly fine. But the validator also lets me write stuff with multiple decimal separators. Strings like 123.567,890 or ,,03.4... get validated but can't get converted into a float.
Is there a way to tell QDoubleValidator to only validate real numbers and not just strings without alphabetical characters?
I basically want to have a validator, that only validates strings, that can get converted to floats
using either the default locale or the german locale.
I have not used the QDoubleValidator so far but I could achieve such behaviour by using a QRegExpValidator:
QRegExpValidator* rxv = new QRegExpValidator(QRegExp("[+-]?\\d*[\\.,]?\\d+"), this);
lineedit->setValidator(rxv);
If you want only convert your content into the float and you don't want locale specifications, you can use QRegExpValidator with next deep regexp.
ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")));
I need a little help with my calculator program. I have created the code for the main buttons like the numbers 0-9 and the arithmetic operators to make it perform simple calculations.
What I'm having problems with right now is making the CE button work, after clicking the CE button I need the last entered character to be removed from the display label.
I have tried to adapt this code somehow, but it doesn't work:
lblResult->substr(0, lblResult->size()-1);
I know I'm doing somehting wrong here, can you please help me?
Thanks in advance
...Now that we know that lblResult is a System.Windows.Forms.Label, we can look at the documentation.
A Label has a Text Property, which is a String^ (i.e. a string reference).
For what you want to do, the Remove Method of String is appropriate. But note in the documentation it says that it "Returns a new string in which a specified number of characters from the current string are deleted." This means that it does not modify the string, but returns a modified copy.
So to change the label's text, we need to assign to its Text property what we want: the current string with all of the characters except the last:
lblResult->Text = lblResult->Text->Remove(lblResult->Text->Length - 1);
lblResult->resize(lblResult->size() - 1);
In this case you can use components Remove and Length methods.
Use the following code to access the components text:
component->Text
Than remove the last character of string by accessing Remove and component Length method
= component->Text->Remove(component->Text->Length - 1)
I hope you find this useful.
Just asking the obvious -- the whole statement is
*lblResult = lblResult->substr(0, lblResult->size()-1);
right?
How in qt constrain what can be typed in lineedit? I would i.e. want user to enter just digits but not letters.
Check the setValidator function and the inputMask property. If you need something more complex than just numbers it is very easy to subclass QValidator and use it.
I have a question about formatting a decimal number to a certain QString format. Basically, I have an input box in my program that can take any values. I want it to translate the value in this box to the format "+05.30" (based on the value). The value will be limited to +/-99.99.
Some examples include:
.2 --> +00.02
-1.5 --> -01.50
9.9 --> +09.90
I'm thinking of using a converter like this, but it will have some obvious issues (no leading 0, no leading + sign).
QString temp = QString::number(ui.m_txtPreX1->text().toDouble(), 'f', 2);
This question had some similarities, but doesn't tie together both front and back end padding.
Convert an int to a QString with zero padding (leading zeroes)
Any ideas of how to approach this problem? Your help is appreciated! Thanks!
I don't think you can do that with any QString method alone (either number or arg). Of course you could add zeros and signs manually, but I would use the good old sprintf:
double value = 1.5;
QString text;
text.sprintf("%+06.2f", value);
Edit: Simplified the code according to alexisdm's comment.
You just have to add the sign manually:
QString("%1%2").arg(x < 0 ? '-' : '+').arg(qFabs(x),5,'f',2,'0');
Edit: The worst thing is that there is actually an internal function, QLocalePrivate:doubleToString that supports the forced sign and the padding at both end as the same time but it is only used with these options in QString::sprintf, and not:
QTextStream and its << operator, which can force the sign to show, but not the width or
QString::arg, which can force the width but not the sign.
But for QTextStream that might be a bug.