Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Save function in Qt Example

    General and Desktop
    save load qtablewidget
    3
    7
    2362
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      A Former User last edited by

      Hi,

      i use the Qt Example "Item Model Example".
      When i change the values of the QTableWidget, the Bars are changing too. That's good.

      How can i save the values and the 3D Bars and open that? Because when i change the values i want to save this and for example open the file in school for a presentation...

      Thanks,
      Henrik

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Do you mean save the state of the model and reload it ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • ?
          A Former User last edited by

          Yes + the TableWidget

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Then that's something you'll have to implement by hand. One possibility is to add a function to dump its content in a text file (e.g. csv) and reload it from there.

            What exact example are you hacking on ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • VRonin
              VRonin last edited by VRonin

              Disclaimer: this is NOT in any way a generic way to serialise (save) a model but it should work for you. It saves only the Qt::DisplayRole of a table model (no child in any index) in a csv file (readable by excel and LibreOffice calc)

              #include <QAbstractItemModel>
              #include <QSaveFile>
              #include <QFile>
              #include <QTextStream>
              bool saveToCsv(const QAbstractItemModel* const model, const QString& destinationPath, const QChar& separator=','){
                  if(!model){
                      //Null model
                      return false;
                  }
                  if(separator.isNull()){
                      // invalid separator
                      return false;
                  }
                  QSaveFile destinationFile(destinationPath);
                  if(!destinationFile.open(QIODevice::WriteOnly | QIODevice::Text)){
                      //invalid destination
                      return false;
                  }
                  QTextStream out(&destinationFile);
                  for(int i=0;i<model->rowCount();++i){
                      for(int j=0;j<model->columnCount();++j){
                          if(j>0)
                              out << separator;
                          out << model->index(i,j).data().toString();
                      }
                      out << '\n';
                  }
                  return  destinationFile.commit();
              }
              bool loadFromCsv(QAbstractItemModel* const model, const QString& sourcePath, const QChar& separator=','){
                  if(!model){
                      //Null model
                      return false;
                  }
                  if(separator.isNull()){
                      // invalid separator
                      return false;
                  }
                  QFile sourceFile(sourcePath);
                  if(!sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)){
                      //invalid source
                      return false;
                  }
                  QTextStream in(&sourceFile);
                  model->removeColumns(0,model->columnCount());
                  model->removeRows(0,model->rowCount());
                  QStringList lineParts;
                  QString tempLine;
                  while (in.readLineInto(&tempLine)) {
                      lineParts=tempLine.split(separator,QString::KeepEmptyParts);
                      if(model->columnCount()==0)
                          model->insertColumns(0,lineParts.size());
                      else if(model->columnCount()!=lineParts.size()){
                          // different rows have different number of columns
                          return false;
                      }
                      const int newRow= model->rowCount();
                      model->insertRow(newRow);
                      for(int i=0;i<lineParts.size();++i)
                          model->setData(model->index(newRow,i),lineParts.at(i));
                  }
                  return true;
              }
              

              You can call this using something like saveToCsv(m_tableWidget->model(),"saved.csv"); and loadFromCsv(m_tableWidget->model(),"saved.csv");

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply Reply Quote 2
              • ?
                A Former User last edited by

                Thanks.
                Can you give me an Example with an SaveFile Dialog and OpenFileDialog which i an use in my Project?

                Henrik

                1 Reply Last reply Reply Quote 0
                • VRonin
                  VRonin last edited by

                  saveToCsv(m_tableWidget->model(),
                  QFileDialog::getSaveFileName(this,tr("Save to csv"),QString(),tr("Comma separated values (*.csv)"))
                  );
                  loadFromCsv(m_tableWidget->model(),
                  QFileDialog::getOpenFileName(this, tr("Open csv"),QString(),tr("Comma separated values (*.csv)"))
                  );
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post