It is often problematic to reuse variables beyond your immediate responsibility. Do not you see anything weird here?
QFile filea("file.txt");
file.open(QIODevice::ReadOnly);
QTextStream texto(&filea);
QDebug<<texto.readAll();
Come on, a clue:
QFile filea("file.txt");
file.open(QIODevice::ReadOnly); // <<--- AQUI
QTextStream texto(&filea);
QDebug<<texto.readAll();
You declare the variable filea
with the intention of reading the data you have previously saved but ... then you set to configure file
, the object used to save the value in the file ...
To avoid this kind of problems, the ideal is to have the smallest possible functions and, if this is not possible, reduce the scope of the variables to the maximum:
ui->setupUi(this);
tiempo=new QTimer(this);
connect(tiempo, SIGNAL(timeout()), this, SLOT(archivo()));
QString text1;
{
int n;
QFile file("file.txt");
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream text(&file);
text1="hola";
n=5;
text<<n;
file.close();
}
{
QFile filea("file.txt");
file.open(QIODevice::ReadOnly); // ahora aqui tenemos error de compilación
QTextStream texto(&filea);
QDebug<<texto.readAll();
}
Apart from that, if the first thing that contains the file is the integer to read, the algorithm to use is as complicated as the following:
int m;
QFile filea("file.txt");
filea.open(QIODevice::ReadOnly); // <<--- AQUI
QTextStream texto(&filea);
filea >> m;
You have already read the value ... but what you want is to use it in another function different ... you have several options but with the little code that you teach I would recommend creating a member variable:
class MainWindow
{
int m;
};
MainWindow::MainWindow()
: m(0) // Repasa todos los constructores
{ }
// int m; ya tenemos la variable miembro, esta no es necesaria
QFile filea("file.txt");
filea.open(QIODevice::ReadOnly); // <<--- AQUI
QTextStream texto(&filea);
filea >> m;
With this your function should already give you the expected result:
void MainWindow::on_pushButton_clicked()
{
ui->spinBox->setValue(m);
}