Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save function in Qt Example
QtWS25 Last Chance

Save function in Qt Example

Scheduled Pinned Locked Moved Unsolved General and Desktop
saveloadqtablewidget
7 Posts 3 Posters 2.8k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Yes + the TableWidget

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            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
            2
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7
                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved