Create a .txt or another format from a listView, Save and load button

0

I have a small program where I add delete and look for elements in a listView, but now I would like to add a couple of buttons to save and load the listView in a text format or any other, so I can use it in later sessions even though I find various problems looking for possible solutions on the web, I'm still a novice, this is my code:

-

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringListModel>
#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->listView->setModel(new QStringListModel(stringList));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_addButton_released()
{
    if (ui->lineEdit->text().isEmpty()|| stringList.contains(ui->lineEdit->text())) {
        QMessageBox::information(this, tr("Empty Field"),
            tr("Por favor, introduce un nuevo codigo o el codigo ya existe."));
        return;
    }else{
    stringList.append(ui->lineEdit->text());
           ((QStringListModel*) ui->listView->model())->setStringList(stringList);
           ui->lineEdit->clear();
    }
}

void MainWindow::on_removeButton_released()
{
    if (ui->lineEdit->text().isEmpty() || !stringList.contains(ui->lineEdit->text())) {
        QMessageBox::information(this, tr("Empty Field"),
            tr("El codigo ha sido ya borrado o no existe."));
        return;
    }else{
    stringList.removeOne(ui->lineEdit->text());
    ((QStringListModel*) ui->listView->model())->setStringList(stringList);
    ui->lineEdit->clear();
    }
}

void MainWindow::on_findButton_released()
{
    if (ui->lineEdit->text().isEmpty()|| !stringList.contains(ui->lineEdit->text())) {
        QMessageBox::information(this, tr("Empty Field"),
            tr("CODIGO NO ENCONTRADO"));
        return;
    }else{
        QMessageBox::information(this, tr("Empty Field"),
            tr("CODIDO ENCONTRADO!"));
    }
}

void MainWindow::on_saveButton_released()
{
}

void MainWindow::on_loadButton_released()
{
}

Someone has an idea where to go

    
asked by Diego Anton Inelmatic Electron 07.08.2017 в 13:42
source

1 answer

1

If we assume that each item in the list will be found on a different line within the file, the reading is more or less trivial. If we look at the documentation of QStringListModel , we see that we can get / set the list of items using a QStringList . The idea is to dump the contents of the file to QStringList and then pass that object to the model:

QStringList LeerDelFichero(
  QString nombreFichero)
{
  QStringList toReturn;

  QFile textFile(nombreFichero);
  if(!textFile.open(QIODevice::ReadOnly)) {
    QMessageBox::information(0,"Error",textFile.errorString());
  }
  else
  {
    QTextStream textStream(&textFile);
    while (true)
    {
      QString line = textStream.readLine();
      if (line.isNull())
         break; 
      else
         toReturn.append(line);
    }
  }

  return toReturn;
}

void MainWindow::on_loadButton_released()
{
  QStringListModel* model = static_cast<QStringListModel*>(ui->listView->model());
  model->setStringList(LeerDelFichero("ruta_del_fichero"));
}

And writing more of the same ... we retrieve the list of elements in QStringList format and dump the content of that list into the file:

void EscribirEnFichero(
  QString nombreFichero,
  QStringList const& listaElementos)
{
  QFile textFile(nombreFichero);
  if(!textFile.open(QIODevice::WriteOnly)) {
    QMessageBox::information(0,"Error",textFile.errorString());
  }
  else
  {
    QTextStream textStream(&textFile);
    for( QString const& item : listaElementos)
    {
      textStream << item << '\n';
    }
  }
}

void MainWindow::on_saveButton_released()
{
  QStringListModel* model = static_cast<QStringListModel*>(ui->listView->model());
  EscribirEnFichero("ruta_del_fichero",model->stringList());
}
    
answered by 08.08.2017 / 14:24
source