Saving or Loading from QJson not working (?)
-
Hi all, Im new to the use of QJson and Im learning it with a loading/saving bookmarks browser feature.
Im working in windows.
I included this in the .h :
...#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QVariantList>
#include <QVariant>
#include <QMetaType>
...This is the code for the save:
void BookMarksBar::saveBookMarks() {
qDebug()<<"Saving"; QFile bookMarksFile("bkms.dat"); if (!bookMarksFile.open(QFile::WriteOnly | QFile::Truncate)) { bookMarksFile.close(); return; } QJsonArray list = QJsonArray::fromVariantList(ToQVariantList(bookMarksList_)); QJsonDocument saveDoc(list); for (int i=0; i<ToQVariantList(bookMarksList_).size(); i++) { qDebug()<<ToQVariantList(bookMarksList_).at(i).value<BookMark>().getIcon(); qDebug()<<ToQVariantList(bookMarksList_).at(i).value<BookMark>().getName(); qDebug()<<ToQVariantList(bookMarksList_).at(i).value<BookMark>().getUrl(); } bookMarksFile.write(saveDoc.toBinaryData()); bookMarksFile.close();
}
And this is the load function:
void BookMarksBar::loadBookMarks() {
qDebug()<<"Loading"; QFile bookMarksFile("bkms.dat"); if (!bookMarksFile.open(QFile::ReadOnly) || bookMarksFile.atEnd()) { bookMarksFile.close(); return; } QJsonDocument loadDoc(QJsonDocument::fromBinaryData(bookMarksFile.readAll())); bookMarksList_ = FromQVariantList(loadDoc.array().toVariantList()); for (int i=0; i<bookMarksList_.size(); i++) { qDebug()<<bookMarksList_[i].getIcon(); qDebug()<<bookMarksList_[i].getName(); qDebug()<<bookMarksList_[i].getUrl(); } for (int i=0; i<bookMarksList_.size(); i++) addBookMark(bookMarksList_[i].getIcon(), bookMarksList_[i].getName(), bookMarksList_[i].getUrl()); bookMarksFile.close();
}
When I use the save function, I get this output as expected:
Saving
QIcon(null)
"DuckDuckGo"
QUrl( "https://duckduckgo.com/" )But, when I load, I get nothing:
Readed
QIcon(null)
""
QUrl( "" )Just in case you need to know, this is from the class (BookMarksBar):
private:
QList<BookMark> bookMarksList_; QVariantList ToQVariantList(const QList<BookMark> &list); QList<BookMark> FromQVariantList(const QVariantList &list);
And those two functions:
QVariantList BookMarksBar::ToQVariantList(const QList<BookMark> &list) {
QVariantList newList; QVariant newVar; for (int i=0; i<list.size(); i++) { newVar.setValue(list[i]); newList.append(newVar); } return newList;
}
QList<BookMark> BookMarksBar::FromQVariantList(const QVariantList &list) {
QList<BookMark> newList; for (int i=0; i<list.size(); i++) newList.append(list[i].value<BookMark>()); return newList;
}
Thanks
-
@mcosta I have just tried it and yes, the problem is here.
This is the .json file:
[
null
]But what is worng in the code? Im just saving a QVariantList
QJsonArray list = QJsonArray::fromVariantList(ToQVariantList(bookMarksList_));
QJsonDocument saveDoc(list); -
I tried this:
typedef struct
{
QString i;
} stBookMark;Q_DECLARE_METATYPE(stBookMark)
In .cpp:
stBookMark x;
x.i = "Hello";QVariant c;
c.setValue(x);QVariantList a;
a.append(c);
a.append(0);
a.append(-119);qDebug()<<a;
QJsonArray list = QJsonArray::fromVariantList(a);
QJsonDocument saveDoc(list);
qDebug() output: (QVariant(stBookMark, ) , QVariant(int, 0) , QVariant(int, -119) )And the output .json file here:
[
null,
0,
-119
] -
Hi,
IMHO the problem is thatQJsonValue QJsonValue::​fromVariant(const QVariant & variant)
(http://doc.qt.io/qt-5/qjsonvalue.html#fromVariant) can't convert BookMark to QJsonObject. I don't think that adding a custom converter with QMetatype::registerConverter() would help. Looks like you have to do the conversion to QJsonArray yourself.
Cheers!
Wieland