Qt handling of QFile files

2

I'm starting in QtCreator and I'm trying to manage files. I want to save a int in a file, and when I press a button it shows me that number in QSpinBox but I do not know how to return the type character int .

My test code:

  • m would be the value of the file I want something like m=qdebug , but I do not know how I can do it.
  ui->setupUi(this);
  tiempo=new QTimer(this);
  connect(tiempo, SIGNAL(timeout()), this, SLOT(archivo()));
  QString text1;
  int n, m;
  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);
  QTextStream texto(&filea);
  QDebug<<texto.readAll();
  //m=Qdebug; o algo que me permita mostrar el contenido como int
}

void MainWindow::on_pushButton_clicked()
{
  ui->spinBox->setValue(m);
}
    
asked by Sebas Salazar 11.06.2017 в 07:41
source

2 answers

1

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);
}
    
answered by 12.06.2017 в 08:43
1

It's easier than all that, but the first thing you should consider when using a file to store information is how you will structure it and then use the appropriate methods. If you are going to keep information of simple values like the content in several spin boxes I recommend that you use the QSettings (Ini files of all the life). So this is the code for the Read button and the Save button:

#include <QSettings>

void MainWindow::on_Leer_clicked() {

    QSettings oSet("file.ini", QSettings::IniFormat);
    int m=oSet.value("Dato1").toInt();
    ui->spinBox->setValue(m);
}

void MainWindow::on_Guardar_clicked() {

    QSettings oSet("file.ini", QSettings::IniFormat);
    QString m=QString::number(ui->spinBox->value());
    oSet.setValue("Dato spin",m);
}
    
answered by 22.06.2017 в 17:24