QString 在追加 10 个字符后重置
QString Resets after appending 10 characters
我刚开始使用QT Language。当我尝试向其附加一个字符 10 次时发生了一个奇怪的错误。它会重置并重新开始。有人知道解决方案吗?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<string>
#include<iostream>
// First Number / Second Number
double num1, num2;
// Action s-substract a-add n-none
char act;
// Result
double result;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_16_clicked()
{
// Test Button
AppendToLabel(2);
}
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);
}
当我附加第 11 个字符时,它会替换整个字符串,而不是将其附加到现有字符串。
我用完整代码更新了问题。需要重现错误。当我按下按钮 10 次时,它正确地将标签附加为“2222222222”。但是我一按11,它就变成了“2”。
任何 QString
值的大小上限为 4GB。
您应该在您的代码中找到错误...
QString::toInt()
和QString::toDouble()
如果不能转换可以return零,设置一个bool参数来检查是否转换成功像这样
bool flag;
int v = tempLabel.toInt(&flag);
一个 32 位整数值可以有最大值 2^31 - 1
,或 2147483647
,恰好是 10 位长。因此,调用 toInt()
.
时,11 位数字会失败
QString s = "12345678901"
qDebug() << s.toInt(); // prints '0'
我刚开始使用QT Language。当我尝试向其附加一个字符 10 次时发生了一个奇怪的错误。它会重置并重新开始。有人知道解决方案吗?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<string>
#include<iostream>
// First Number / Second Number
double num1, num2;
// Action s-substract a-add n-none
char act;
// Result
double result;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_16_clicked()
{
// Test Button
AppendToLabel(2);
}
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);
}
当我附加第 11 个字符时,它会替换整个字符串,而不是将其附加到现有字符串。
我用完整代码更新了问题。需要重现错误。当我按下按钮 10 次时,它正确地将标签附加为“2222222222”。但是我一按11,它就变成了“2”。
任何 QString
值的大小上限为 4GB。
您应该在您的代码中找到错误...
QString::toInt()
和QString::toDouble()
如果不能转换可以return零,设置一个bool参数来检查是否转换成功像这样
bool flag;
int v = tempLabel.toInt(&flag);
一个 32 位整数值可以有最大值 2^31 - 1
,或 2147483647
,恰好是 10 位长。因此,调用 toInt()
.
QString s = "12345678901"
qDebug() << s.toInt(); // prints '0'