Text_Changed
-
I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""
void MainWindow::on_Value_textChanged(const QString &arg1) { if(arg1.toDouble()>30 || arg1.toDouble()<-30) { ui->textBrowser->setText("b");
} else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30) { ui->Led->setPower(1); } // //se la mia lineEdit è vuota setto i valori di dial e slider a zero else if (arg1 == "") { ui->textBrowser->setText("aa");
}
-
I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""
void MainWindow::on_Value_textChanged(const QString &arg1) { if(arg1.toDouble()>30 || arg1.toDouble()<-30) { ui->textBrowser->setText("b");
} else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30) { ui->Led->setPower(1); } // //se la mia lineEdit è vuota setto i valori di dial e slider a zero else if (arg1 == "") { ui->textBrowser->setText("aa");
}
@vale88 Try
else if (arg1.isEmpty())
instead.
Also you should check first whether it is empty, because it does not make sense to convert an empty string to double.
And you should do the conversion only once to make the code faster. -
I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""
void MainWindow::on_Value_textChanged(const QString &arg1) { if(arg1.toDouble()>30 || arg1.toDouble()<-30) { ui->textBrowser->setText("b");
} else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30) { ui->Led->setPower(1); } // //se la mia lineEdit è vuota setto i valori di dial e slider a zero else if (arg1 == "") { ui->textBrowser->setText("aa");
}
@vale88 you're checking if the String converted to double is between -30 and 30. If the conversion fails, which is the case for an empty string, toDouble will return a 0. That is between -30 and 30
edit example code correction:
bool ok; double value = arg1.toDouble(&ok); if(!ok) ui->textBrowser->setText("aa"); else{ if(value >30 || value <-30) { ui->textBrowser->setText("b"); } else { ui->Led->setPower(1); } }
-
I used textChanged slot but the code soesn't work when arg1=="", in debug I see the cose don't recognize when arg1==""
void MainWindow::on_Value_textChanged(const QString &arg1) { if(arg1.toDouble()>30 || arg1.toDouble()<-30) { ui->textBrowser->setText("b");
} else if(arg1.toDouble()>=-30 && arg1.toDouble()<=30) { ui->Led->setPower(1); } // //se la mia lineEdit è vuota setto i valori di dial e slider a zero else if (arg1 == "") { ui->textBrowser->setText("aa");
}
@vale88 Be aware when using QString and "":
QString empty; QString empty2(""); empty == "" ==> is false empty2 == "" ==> is true emtpy == empty2 ==> is false!!
I would first check if arg1 not empty, then if arg1 is a double value
void MainWindow::on_Value_textChanged(const QString &arg1) { if(arg1.isEmpty()) { // ==> do something return; } bool ok; double value = arg1.touDouble(&ok); if(!ok) { // ==> arg1 not a double value... return; } if(value >30 || value <-30) { ui->textBrowser->setText("b"); } else { ui->Led->setPower(1); } }
-
@vale88 you're checking if the String converted to double is between -30 and 30. If the conversion fails, which is the case for an empty string, toDouble will return a 0. That is between -30 and 30
edit example code correction:
bool ok; double value = arg1.toDouble(&ok); if(!ok) ui->textBrowser->setText("aa"); else{ if(value >30 || value <-30) { ui->textBrowser->setText("b"); } else { ui->Led->setPower(1); } }
@J.Hilk thenks..it works..I didn't undestand that when arg1=="" the result of conversion in 0
-
@J.Hilk thenks..it works..I didn't undestand that when arg1=="" the result of conversion in 0
@vale88 said in Text_Changed:
I didn't undestand that when arg1=="" the result of conversion in 0
It is the way how QString::toDouble() works ==> cf https://doc.qt.io/Qt-5/qstring.html#toDouble
- Returns the string converted to a double value.
- Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).
-
@vale88 said in Text_Changed:
I didn't undestand that when arg1=="" the result of conversion in 0
It is the way how QString::toDouble() works ==> cf https://doc.qt.io/Qt-5/qstring.html#toDouble
- Returns the string converted to a double value.
- Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).
@KroMignon thanks