【发布时间】:2021-10-21 11:09:06
【问题描述】:
我刚开始使用 QT 语言。当我尝试将一个字符附加 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”。
【问题讨论】:
-
此问题显示的代码不符合 stackoverflow.com 对 minimal reproducible example 的要求。这意味着这里的任何人都不太可能最终回答这个问题。但最多只能猜测。你应该edit你的问题来展示一个最小的例子,不超过一两页代码(“最小”部分),其他人都可以剪切/粘贴、编译、运行和重现所描述的问题(“ reproducible”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
-
“2147483647”有多少个字符?
-
此重载适用于 64 位整数,但您需要将 32 位整数更改为 64 位整数:https://doc.qt.io/qt-5.15/qstring.html#number-5