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. XML or JSON using QML?
Forum Updated to NodeBB v4.3 + New Features

XML or JSON using QML?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.7k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Anas A. Ismail
    wrote on last edited by Anas A. Ismail
    #1

    Hello everyone,
    I'm working on a simple task for saving and loading data using QML, which type do you recommend to use XML or JSON, which one in well-supported in QML .. ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      QML supports well XML (XmlListModel) and JSON (native in JavaScript). Anyway IIRC there's no support to save and load data from QML except if you use Local Storage

      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/)

      A 1 Reply Last reply
      0
      • M mcosta

        Hi,

        QML supports well XML (XmlListModel) and JSON (native in JavaScript). Anyway IIRC there's no support to save and load data from QML except if you use Local Storage

        A Offline
        A Offline
        Anas A. Ismail
        wrote on last edited by
        #3

        @mcosta So, I can't save XML data using XmlListModel ?

        1 Reply Last reply
        0
        • Hamed.MasafiH Offline
          Hamed.MasafiH Offline
          Hamed.Masafi
          wrote on last edited by
          #4

          You can create a simple c++ class that save and load QString from/to file and post in to qml with qmlRegisterType, then you can save and load json (as mcosta says qml/js support it) into file.

          Remote object sharing (OO RPC)
          http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

          Advanced, Powerful and easy to use ORM for Qt5
          https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

          A 1 Reply Last reply
          1
          • Hamed.MasafiH Hamed.Masafi

            You can create a simple c++ class that save and load QString from/to file and post in to qml with qmlRegisterType, then you can save and load json (as mcosta says qml/js support it) into file.

            A Offline
            A Offline
            Anas A. Ismail
            wrote on last edited by
            #5

            @Hamed.Masafi Thanks.
            May you provide me with starter code for this as I'm new with this ?

            1 Reply Last reply
            0
            • Hamed.MasafiH Offline
              Hamed.MasafiH Offline
              Hamed.Masafi
              wrote on last edited by Hamed.Masafi
              #6

              Create a new QtQuick application project and add a class named JsonStorage to it.
              Sources:

              jsonstorage.h:

              #ifndef JSONSTORAGE_H
              #define JSONSTORAGE_H
              
              #include <QtCore/qglobal.h>
              #include <QQuickItem>
              
              class JsonStorage : public QQuickItem
              {
                  Q_OBJECT
                  Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
              
                  QString m_fileName;
              
              public:
                  JsonStorage();
              
                  Q_INVOKABLE void save(QString data);
                  Q_INVOKABLE QString load();
                  QString fileName() const;
              
              signals:
              
                  void fileNameChanged(QString fileName);
              
              public slots:
                  void setFileName(QString fileName);
              };
              
              #endif // JSONSTORAGE_H
              

              jsonstorage.cpp

              #include <QFile>
              
              #include "jsonstorage.h"
              
              JsonStorage::JsonStorage()
              {
              
              }
              
              void JsonStorage::save(QString data)
              {
                  QFile f(fileName());
                  if(!f.open(QIODevice::WriteOnly))
                      return;
              
                  QByteArray ba = data.toUtf8();
                  f.write(ba.data(), ba.length());
                  f.close();
              }
              
              QString JsonStorage::load()
              {
                  QFile f(fileName());
                  if(!f.open(QIODevice::ReadOnly))
                      return "";
              
                  QByteArray ba = f.readAll();
                  f.close();
                  return QString(ba);
              }
              
              QString JsonStorage::fileName() const
              {
                  return m_fileName;
              }
              
              void JsonStorage::setFileName(QString fileName)
              {
                  if (m_fileName == fileName)
                      return;
              
                  m_fileName = fileName;
                  emit fileNameChanged(fileName);
              }
              

              main.cpp:

              #include <QApplication>
              #include <QQmlApplicationEngine>
              
              #include "jsonstorage.h"
              
              int main(int argc, char *argv[])
              {
                  QApplication app(argc, argv);
              
                  qmlRegisterType<JsonStorage>("org.Qt.io", 1, 0, "JsonStorage");
              
                  QQmlApplicationEngine engine;
                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
                  return app.exec();
              }
              

              main.qml:

              import QtQuick 2.4
              import QtQuick.Controls 1.3
              import QtQuick.Window 2.2
              import QtQuick.Dialogs 1.2
              import org.Qt.io 1.0
              import QtQuick.Layouts 1.2
              
              ApplicationWindow {
                  title: qsTr("Hello World")
                  width: 640
                  height: 480
                  visible: true
              
                  JsonStorage{
                      id: storage
                      fileName: "db"
                  }
              
                  ColumnLayout {
                      anchors.centerIn: parent
              
                      Label {
                          text: "Name:"
                          Layout.fillWidth: true
                      }
              
                      TextField {
                          id: textFieldName
                          Layout.fillWidth: true
                      }
              
                      Label {
                          text: "Last name:"
                          Layout.fillWidth: true
                      }
              
                      TextField {
                          id: textFieldLastName
                          Layout.fillWidth: true
                      }
              
                      RowLayout{
                          Button{
                              text: "Save"
                              onClicked: {
                                  var o = {
                                      name: textFieldName.text,
                                      lastName: textFieldLastName.text
                                  }
                                  var data = JSON.stringify(o)
                                  storage.save(data)
                              }
                          }
                          Button{
                              text: "Load"
              
                              onClicked: {
                                  var data = JSON.parse(storage.load())
              
                                  textFieldName.text = data.name
                                  textFieldLastName.text = data.lastName
                              }
                          }
                      }
              
              
                  }
              }
              

              I have tested, it works fine.

              Remote object sharing (OO RPC)
              http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

              Advanced, Powerful and easy to use ORM for Qt5
              https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

              A 1 Reply Last reply
              1
              • Hamed.MasafiH Hamed.Masafi

                Create a new QtQuick application project and add a class named JsonStorage to it.
                Sources:

                jsonstorage.h:

                #ifndef JSONSTORAGE_H
                #define JSONSTORAGE_H
                
                #include <QtCore/qglobal.h>
                #include <QQuickItem>
                
                class JsonStorage : public QQuickItem
                {
                    Q_OBJECT
                    Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
                
                    QString m_fileName;
                
                public:
                    JsonStorage();
                
                    Q_INVOKABLE void save(QString data);
                    Q_INVOKABLE QString load();
                    QString fileName() const;
                
                signals:
                
                    void fileNameChanged(QString fileName);
                
                public slots:
                    void setFileName(QString fileName);
                };
                
                #endif // JSONSTORAGE_H
                

                jsonstorage.cpp

                #include <QFile>
                
                #include "jsonstorage.h"
                
                JsonStorage::JsonStorage()
                {
                
                }
                
                void JsonStorage::save(QString data)
                {
                    QFile f(fileName());
                    if(!f.open(QIODevice::WriteOnly))
                        return;
                
                    QByteArray ba = data.toUtf8();
                    f.write(ba.data(), ba.length());
                    f.close();
                }
                
                QString JsonStorage::load()
                {
                    QFile f(fileName());
                    if(!f.open(QIODevice::ReadOnly))
                        return "";
                
                    QByteArray ba = f.readAll();
                    f.close();
                    return QString(ba);
                }
                
                QString JsonStorage::fileName() const
                {
                    return m_fileName;
                }
                
                void JsonStorage::setFileName(QString fileName)
                {
                    if (m_fileName == fileName)
                        return;
                
                    m_fileName = fileName;
                    emit fileNameChanged(fileName);
                }
                

                main.cpp:

                #include <QApplication>
                #include <QQmlApplicationEngine>
                
                #include "jsonstorage.h"
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                
                    qmlRegisterType<JsonStorage>("org.Qt.io", 1, 0, "JsonStorage");
                
                    QQmlApplicationEngine engine;
                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                
                    return app.exec();
                }
                

                main.qml:

                import QtQuick 2.4
                import QtQuick.Controls 1.3
                import QtQuick.Window 2.2
                import QtQuick.Dialogs 1.2
                import org.Qt.io 1.0
                import QtQuick.Layouts 1.2
                
                ApplicationWindow {
                    title: qsTr("Hello World")
                    width: 640
                    height: 480
                    visible: true
                
                    JsonStorage{
                        id: storage
                        fileName: "db"
                    }
                
                    ColumnLayout {
                        anchors.centerIn: parent
                
                        Label {
                            text: "Name:"
                            Layout.fillWidth: true
                        }
                
                        TextField {
                            id: textFieldName
                            Layout.fillWidth: true
                        }
                
                        Label {
                            text: "Last name:"
                            Layout.fillWidth: true
                        }
                
                        TextField {
                            id: textFieldLastName
                            Layout.fillWidth: true
                        }
                
                        RowLayout{
                            Button{
                                text: "Save"
                                onClicked: {
                                    var o = {
                                        name: textFieldName.text,
                                        lastName: textFieldLastName.text
                                    }
                                    var data = JSON.stringify(o)
                                    storage.save(data)
                                }
                            }
                            Button{
                                text: "Load"
                
                                onClicked: {
                                    var data = JSON.parse(storage.load())
                
                                    textFieldName.text = data.name
                                    textFieldLastName.text = data.lastName
                                }
                            }
                        }
                
                
                    }
                }
                

                I have tested, it works fine.

                A Offline
                A Offline
                Anas A. Ismail
                wrote on last edited by
                #7

                @Hamed.Masafi Thanks, I do appreciate your help.

                1 Reply Last reply
                0

                • Login

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