void table::Save(QString filePath) { if (m_model->smthChanged || filePath != "") { if (m_fileName == "" && filePath == "") { SaveAs(); return; } if (filePath == "") { filePath = m_fileName; } QFile f(filePath); if(f.open(QIODevice::WriteOnly)) { QVariantMap data; for (int i = 0; i < m_model->rowCount(QModelIndex()); ++i) { data.insert(m_model->data(m_model->index(i, 0), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, 1), Qt::DisplayRole).toInt()); } QJsonDocument doc; f.write(doc.fromVariant(data).toJson()); f.close(); } m_fileName = filePath; m_model->smthChanged = false; ui->actionSave->setEnabled(false); } }
bool MainWindow::Save() { QFile json(m_fileName); if(json.open(QIODevice::WriteOnly)) { QJsonDocument doc; QList<Population> modelData = m_model->GetData(); QVariantMap data; for (int i = 0; i < modelData.count(); ++i) { data.insert(modelData[i].GetCountry(), modelData[i].GetPopulation()); } json.write(doc.fromVariant(data).toJson()); json.close(); m_isSomethingChanged = false; return true; } return false; }