I have a LineEdit which I want it to present a float value. I want the float value to have 2 digits precision so I used number function like this:
float tax = value * 0.23;
Qstring strTax = QString::number(tax, 'f', 2);
qDebug() << strTax;
ui->leTax->setText(strTax);
The thing is that while in console the value is printed with 2 digits precision, the widget prints all the decimal digits which might be 3 or more (depends on the value). Is there a way to fix it? I am using Qt 5.0.
So this is the accepted answer. I finally solved my problem. The onTextUpdate had to update two more LineEdits one containing the Tax and one containing The total amount. But the one containing the totalAmount also emitted the onTextChanged to update the net value and the Tax LineEdits, without rounding the values(I Was careless!!). So I corrected the totalAmount's onTextChanged. I also updated it to check if it has focus so to know if it is its turn to update the other LineEdits or not :). The point was that someone could edit the netValue line edit and that would update the tax and total amount or someone would enter the totalAmount and that automatically would update the net Amount and tax field. Everything working now. Thank you all for answering!!
I do not have a float-version for QString::number(), so maybe try cast-to-double
ui->leTax->setText(QString::number((double)tax, 'f', 2));
But since qDebug() is already showing the correct value, you probably change your strTax somewhere else in the code.
setValidator might be your solution. Click here and here
void QLineEdit::setValidator ( const QValidator * v )
Sets this line edit to only accept input that the validator, v, will accept. This allows you to place any arbitrary constraints on the text which may be entered.
Also, Set the number of decimal places with setDecimals().
So, it should looks like this :
// bottom (-999.0), top (999.0), decimals (2)
lineEdit->setValidator(new QDoubleValidator(-999.0, 999.0, 2, lineEdit));
Related
I want to find the minimum number with given conditions(is writer and is under probation), the below code works if D contains numbers, but how do I do it if the number is a part of a string, like a fraction for example? Like how do I use this formula if numbers in D look like "1/8", "31/688", "21/33", etc?
=MINIFS(D3:D1007, A3:A1007, "Writer", C3:C1007, "Probation")
I already have another formula that I use that calculates a decimal value given the fraction, If the fraction is in cell D21 then it would look like this:
=left(D21,find("/",D21)-1)/(right(D21,len(D21)-find("/",D21)))
but how do I apply this kind of formula in a minif/maxif?
I have attached a picture to show what I mean, what I'm trying to do is to put a formula in the passed/total column of package stats(probation), and it will get the lowest passed/total value out of the ones with that package name and importance level. as you can see, the entire writer package's pass rate is 5/8 because the lowest pass rate out of the writer package 5/8 is the lowest pass rate out people with package=writers and importance = probation. But at the moment I have to enter the 5/8s manually, I want it to be able to get it automatically using the formula I'm trying to figure out above.
try:
=ARRAYFORMULA(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )))
or to return fraction:
=ARRAYFORMULA(VLOOKUP(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )),
{IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), ), D3:D}, 2, 0))
also make sure fractions are formatted as plain text not date
I can't figure out why this if statement won't work.
I have a DateTime field DATEFROM and a String parameter (it HAS to be String) periodEnd.
I would like to calculate percentages depending if these two dates have 1, 2, 3 or more years difference.
When I use this formula I get either "100%" or "-" and never the other two options. It's like CR calculates the first IF and if it's true then: "100%" but if it's false, it never checks for the rest of the Else Ifs and goes dirreclty to Else
StringVar percentage:="";
If (cDate({?periodEnd})-{TABLE.DATEFROM})<=1 Then
percentage:="100%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=2 Then
percentage:="66%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=3 Then
percentage:="33%"
Else
percentage:="-"
Any idea?
a) I assume you already made sure your periodend format matches with what cdate interprets?
If you just subtract them, Crystal by default returns the number of days between.
Two ways you can do year:
datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM})
or
year(cDate({?periodEnd})-year({TABLE.DATEFROM})
b) You've also no doubt accounted for when your {TABLE.DATEFROM} is greater than cDate({?periodEnd} and you have a negative result?
Not sure if the following is the behavior you would want, but I'm throwing it in for your information
ABS(datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM}))
**make sure you check the help file for the datediff codes ("yyyy" etc) as they are not quite instinctive and can trip you up when you think you're using the obvious one
I am trying to use QlineEdit.
How would I enter a value into the edit bar when I run the program and get that valued stored as a variable to be used later. So far I have only found out how to enter text using
void parameter_settings::on_lineEdit_textEdited(const QString &arg1)
{
ui->lineEdit->setText("");
}
I have a GUI that requires the user to enter a value within a specific range. That value would be stored as a variable for later use. I have read about validators but can't get it to work as intended.
I am not entirely sure what your question is, but you can get the input from a QLineEdit with the command text():
QString input = ui->lineEdit->text();
and an integer input by using:
int integer_value = ui->lineEdit->text().toInt();
As you mentioned validators: You can use validators to allow the user to insert only integers into the QLineEdit in the first place. There are different ones but I generally like to use 'RegEx' validators. In this case:
QRegExpValidator* rxv = new QRegExpValidator(QRegExp("\\d*"), this); // only pos
QRegExpValidator* rxv = new QRegExpValidator(QRegExp("[+-]?\\d*"), this); // pos and neg
ui->lineEdit->setValidator(rxv);
Note: As mentioned in the comments by Pratham, if you only require integers to be entered you should probably use a QSpinBox which does all this out-of-the-box and comes with extra handles to easily increase and decrease of the value.
Use this method:
QString str = QString::number(4.4);
ui->lineEdit->setText(str);
I'm trying to apply validator in a line edit box in Qt 4.2 and it is not working:
QDoubleValidator *haha= new QDoubleValidator(this);
haha->setBottom(0.00);
haha->setDecimals(2);
haha->setTop(100.00);
get_line_edit()->setValidator(haha);
or
QDoubleValidator *haha= new QDoubleValidator(0.00,100.00,2,this);
Neither way, I can still enter what ever value I want.
But if I switch to QIntValidator, it works!
So I went onto Google and did a bit search, and many people used to having the same issue. Is it a bug? or there should be some other set up I should do?
Just tripped over this one. Try setting the QDoubleValidator notation to:
doubleValidator->setNotation(QDoubleValidator::StandardNotation);
The validator documentation says that it returns "Intermediate" from "validate" when the input is an arbitrary double but out of range.
You need to distinguish intermediate input and the final input the user wants to submit by use of a line edit control (e.g. by emitting the "returnPressed" signal). If the user typed "10000" that is still a valid intermediate input for a number between 0 and 100 because the user can prefix this input with "0.".
You have to set notation to your validator
QLineEdit *firstX;
QDoubleValidator* validFirstX = new QDoubleValidator(-1000, 1000, 3, ui.firstX);
validFirstX->setNotation(QDoubleValidator::StandardNotation);
then it works but not fully correct. Interesting part is that it controls the digit numbers not number itself. For example, In this example, you can enter to QLineEdit 1000 either 9999.
&& ( input.toDouble() > top() || input.toDouble() < bottom())
This example works fine in 4.8. It doesn't look like its changed since 4.2 so I suspect the problem lies in how you are creating your QLineEdit. This is the relevent code from that example.
QLineEdit* validatorLineEdit;
validatorLineEdit = new QLineEdit;
validatorLineEdit->setValidator( new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit));
How have you created your line edit?
To clarify, use QDoubleValidator::setNotation(QDoubleValidator::StandardNotation)
Example:
QDoubleValidator* doubleValidator = new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit);
doubleValidator->setNotation(QDoubleValidator::StandardNotation);
validatorLineEdit->setValidator(doubleValidator);
If you set a validator to a QLineEdit then you can use the function hasAcceptableInput() to check whether inputed value is valid or invalid. For example:
if (!ui->lineEdit_planned_count_vrt->hasAcceptableInput())
{
slot_show_notification_message("EDIT_PLAN_COUNT_VRT", notification_types::ERROR, INVALID_INPUTED_VALUE);
return;
}
bool isOk = false;
double value = ui->lineEdit_planned_count_vrt->text().toDouble(&isOk);
//do something with the value here....
I am starting a Qt course this semester. Having looked at the official documentation as well as some on line examples I am confused by the parameter lists of the QInputDialog and QMessagebox classes.
Is there anywhere where one could find some decent info as to what to pass when creating the class / form?
Right now I have this by trial error
tempC = QInputDialog::getDouble(0, "Temperature Converter",
"Enter the temperature in Celsius to convert to Fahrenheit:", 1);
Looking at the official docs doesn't help a lot either (at least not for me yet) as it says this:
double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
as an example.
Any links will be much appreciated.
double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
A dialog will popup with parent the widget in which you are using this function. (this)
The dialog's title will be QInputDialog::getDouble() (tr is used in order to translate this string if you want using QtLinguist)
Inside the dialog will be a double spibox and a label
The label's string will be Amount:
The default value of the spinbox (what you see when the dialog popups) will be 37.56
The minimum value will be -10000 (you will not be able to set a value less than this)
The maximum value will be 10000 (you will not be able to set a value greater than this)
Two decimal point will be displayed, eg 3.478 will be displayed as 3.48.
If the user presses the Ok button then the ok argument will be set to true, otherwise it will be set to false
Check the documentation which includes an example for more details.