Save function in Qt Example



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • Yes + the TableWidget


  • Lifetime Qt Champion

    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 ?



  • 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");



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

    Henrik



  • 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)"))
    );
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.