Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving or Loading from QJson not working (?)

Saving or Loading from QJson not working (?)

Scheduled Pinned Locked Moved General and Desktop
qjsonsaveload
5 Posts 3 Posters 3.2k Views
  • 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 Offline
    E Offline
    Ediolot
    wrote on 12 Mar 2015, 13:34 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mcosta
      wrote on 12 Mar 2015, 13:37 last edited by
      #2

      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 12 Mar 2015, 13:42
      0
      • M mcosta
        12 Mar 2015, 13:37

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

        E Offline
        E Offline
        Ediolot
        wrote on 12 Mar 2015, 13:42 last edited by
        #3

        @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
        0
        • E Offline
          E Offline
          Ediolot
          wrote on 12 Mar 2015, 16:16 last edited by
          #4

          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
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 12 Mar 2015, 16:35 last edited by
            #5

            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
            0

            5/5

            12 Mar 2015, 16:35

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved