Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Saving or Loading from QJson not working (?)

    General and Desktop
    qjson save load
    3
    5
    2704
    Loading More Posts
    • 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.
    • E
      Ediolot last edited by

      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

      1 Reply Last reply Reply Quote 0
      • M
        mcosta last edited by

        With JSON a simple way to Debug is to read the generated JSON and check if it looks like you expect.

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        E 1 Reply Last reply Reply Quote 0
        • E
          Ediolot @mcosta last edited by

          @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);

          1 Reply Last reply Reply Quote 0
          • E
            Ediolot last edited by

            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
            ]

            1 Reply Last reply Reply Quote 0
            • ?
              A Former User last edited by

              Hi,
              IMHO the problem is that QJsonValue 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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post