I just started QT Language. A strange error occurred when I tried appending a character 10 times to it. It resets and starts over. Does anyone know a solution?
QString tempLabel = "0";
void MainWindow::AppendToLabel(int s){
//QString LabelText = ui->displayText->text();
//testCase
QString LabelText = tempLabel;
// Updates the label acording to the button
if(tempLabel.toInt() == 0)
{
// If the Number 0 present in the label - Rplace it
LabelText = QString::number(s);
}
else
{
// If not not - Append it to the label
LabelText.append(QString::number(s));
}
double apendedNumber = LabelText.toDouble();
qDebug() << LabelText;
tempLabel = LabelText;
//ui->displayText->setText(LabelText);
}
When I append the 11th character, it replaces the whole string instead of appending it to the existing one.
CodePudding user response:
A 32-bit integer value can have a maximum value of 2^31 - 1
, or 2147483647
, which happens to be 10 digits long. So an 11 digit number would fail when calling toInt()
.
QString s = "12345678901"
qDebug() << s.toInt(); // prints '0'
CodePudding user response:
The upper size limit for any QString
value is 4GB.
You should find a bug in your code...
QString::toInt()
and QString::toDouble()
can return zero if it can not convert, set a bool parameter to check is converted success like these
bool flag;
int v = tempLabel.toInt(&flag);