How to swap the value from two lineEdit fields
-
Hi,
I want to swap the values from two lineEdit fields.
I have two lineEdit fields, width and length, with an IF statement comparing the values and if width is bigger than length a QMessageBox appears advising the user and everything is working fine, but instead I would like avoid the messageBox and swap the numbers automatically if the user enters a bigger number in the width field.
In other words if the user enters a bigger number for the width than for the length I want to swap the numbers before any calculations.
I tried the following code and it works but it's constantly crashing.
@if(partW.toFloat() > partL.toFloat())
{
QString text = ui->lineEdit_PartLength->text();
QString text2 = ui->lineEdit_PartWidth->text();
ui->lineEdit_PartWidth->setText(text);
ui->lineEdit_PartLength->setText(text2);
}@I know my code is poor and there may be a better solution.
Any idea what would be the best way to add this functionality?
Thanks a lot.
-
Assuming that you are not running out of memory the reason for the crash is not in this part of the code. My blind guess is that the problems are with partW and partL.
As it looks you should have assigned both fields somewhere before. So, if you have pulled already the strings for comparison, there is no need to get them again for the exchange.
Note: this is a lot of guessing involved. You need to show more code. -
You should split the "partW.toFloat() > partL.toFloat()" part into multiple calls in order to be able to check their *ok return value.
Print debug output inside the block. You might create an infinite loop if this code is inside a slot that gets called when the text edits are changed.
-
-
[quote author="koahnig" date="1342967180"]Assuming that you are not running out of memory the reason for the crash is not in this part of the code. My blind guess is that the problems are with partW and partL.
As it looks you should have assigned both fields somewhere before. So, if you have pulled already the strings for comparison, there is no need to get them again for the exchange.
Note: this is a lot of guessing involved. You need to show more code. [/quote]
Wow, it's incredible how good you guys are just by looking at a bit of code you can still give good advise. Even though I haven't fixed it I have a filling that your assumptions are good and it's where I'm going to start.[quote author="DerManu" date="1342969909"]You should split the "partW.toFloat() > partL.toFloat()" part into multiple calls in order to be able to check their *ok return value.
Print debug output inside the block. You might create an infinite loop if this code is inside a slot that gets called when the text edits are changed.[/quote]
Excuse my ignorance but what do you mean with split "partW.toFloat() > partL.toFloat()"? Sorry for the silly question.[quote author="Wilk" date="1342974292"]Hello.
I have a little bit oftopic suggestion: use QDoubleSpinBox instead of QLineEdit. It will make your application more reliable because QDoubleSpinBox automaticaly controlls user input, while QLineEdit doesn't, so you must control it by yourself.[/quote]
This is actually good advice because validating lineEdits are a pain, and the only reason I didn't use QDoubleSpinBox is because in these two fields I didn't like the way it looked with the spinbox, I have 7 input fields and these are the only ones as lineEdits the rest of them are QDoubleSpinBoxes, but I might re-considere changing them to QdoubleSpinBoxes.Thank you all a lot
-
About splitting “partW.toFloat() > partL.toFloat()”:
I guess DerManu means something like this:
@
bool l_ok = false;
bool w_ok = false;double l = partW.toFloat(&l_ok);
double w = partW.toFloat(&w_ok)if (l_ok && w_ok) {
if (w > l) {
QString text = ui->lineEdit_PartLength->text();
QString text2 = ui->lineEdit_PartWidth->text();
ui->lineEdit_PartWidth->setText(text);
ui->lineEdit_PartLength->setText(text2);
}
}
@