What is your suggestion?
-
Hi,
Please give an example of what you are trying to save/reload.
-
A text file or a CSV file. Depending on what you want to do with these data a SQLite database might be a good alternative.
-
Hi
I need help saving data ,
I want to save numbers data that are in dates ,
and then extract this data later.
for example :
i had saved some numbers , now i want check to see what day was best in a year.
What is your suggestion?
Thanks.@Armin said:
I want to save numbers data that are in dates, and then extract this data later.
...
2016-11-25 500
2016-11-26 450
...Here's an example... just one way to do it.
// Some data. QMap<QDate, int> values; values.insert(QDate(2016, 11, 25), 500); values.insert(QDate(2016, 11, 26), 450); values.insert(QDate(2016, 11, 27), 200); values.insert(QDate(2016, 11, 28), 300); values.insert(QDate(2016, 11, 29), 1000); values.insert(QDate(2016, 12, 25), 100); values.insert(QDate(2016, 12, 26), 125); values.insert(QDate(2016, 11, 27), 700); values.insert(QDate(2016, 12, 28), 300); values.insert(QDate(2016, 12, 29), 600); // Write to file. QFile file(QLatin1String("temp.txt")); if (file.open(QFile::WriteOnly)) { QTextStream stream(&file); for (auto iter = values.constBegin(); iter != values.constEnd(); ++iter) { stream << iter.key().toString(Qt::ISODate) << ' ' << QString::number(iter.value()) << '\n'; } } file.close(); // Read from file QMap<QDate, int> readValues; if (file.open(QFile::ReadOnly)) { QTextStream stream(&file); while (!stream.atEnd()) { const QStringList parts = stream.readLine().split(QLatin1Char(' ')); const QStringList dateParts = parts.at(0).split(QLatin1Char('-')); const QDate date = QDate(dateParts.at(0).toInt(), dateParts.at(1).toInt(), dateParts.at(2).toInt()); readValues.insert(date, parts.at(1).toInt()); } } qDebug() << values; qDebug() << readValues; qDebug() << (readValues == values);
This writes, then reads a file containing:
2016-11-25 500 2016-11-26 450 2016-11-27 700 2016-11-28 300 2016-11-29 1000 2016-12-25 100 2016-12-26 125 2016-12-28 300 2016-12-29 600
And also outputs (in debug):
QMap((QDate("2016-11-25"), 500)(QDate("2016-11-26"), 450)(QDate("2016-11-27"), 700)(QDate("2016-11-28"), 300)(QDate("2016-11-29"), 1000)(QDate("2016-12-25"), 100)(QDate("2016-12-26"), 125)(QDate("2016-12-28"), 300)(QDate("2016-12-29"), 600)) QMap((QDate("2016-11-25"), 500)(QDate("2016-11-26"), 450)(QDate("2016-11-27"), 700)(QDate("2016-11-28"), 300)(QDate("2016-11-29"), 1000)(QDate("2016-12-25"), 100)(QDate("2016-12-26"), 125)(QDate("2016-12-28"), 300)(QDate("2016-12-29"), 600)) true
Cheers.
-
@Paul-Colby Wow , Thanks for share , Your answer is fantastic
But I'm beginner and understanding your code is hard for me
I think input or output in Qt is difference with C++
Can i ask a question which what is name of your method writed code?
Thanks