Qt Forum

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

    Unsolved Load data from XML file in TableView and write all record of TableView in XML file

    QML and Qt Quick
    1
    1
    1055
    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.
    • N
      neda last edited by neda

      Hi,
      How can I load data from XML file in Tableview and write all record of Tableview in XML file.
      I can display my XML file using XmlListModel in Tableview, but how can I save all record of Tableview in XML file.
      Please guide me.

      I must write and open a XML file Like this:

      <?xml version="1.0" standalone="yes"?>
      <NewDataSet>
      <record prop1=".."  prop10="..." />  
      .......
        <record prop1=".." prop10="..."/>
      </NewDataSet>
      

      I use QAbstractTableModel, but I do not see any data after open XML file in Tableview.

      
                                  TableView {
      
                                          id: metadataTaskTable
                                          anchors.fill: parent
                                          alternatingRowColors: false
                                          model: theModel
      
                                          // Row Delegate
                                          rowDelegate: Item {
      
                                              height: 20 + 15
                                              Rectangle {
                                                  anchors {
                                                      left: parent.left
                                                      right: parent.right
                                                      verticalCenter: parent.verticalCenter
                                                  }
                                                  height: parent.height
                                                  color: styleData.selected ? 'lightblue' : 'white'
                                              }
                                          }
      
                                          // Column 0
                                          TableViewColumn {
                                              role: "role_enable";
                                              width: 25;
                                              resizable: false;
      
                                              delegate:
                                              CheckBox {
                                                  id: checkBoxDelegate
                                                  anchors.verticalCenter: parent.verticalCenter
                                                  anchors.left: parent.left
                                                  anchors.right: parent.right
                                                  anchors.margins: 5
                                                  checked: styleData.value
      
                                                  onCheckedChanged: {
      
                                                      // Print Model
                                                      theModel.print_rows()
                                                  }
                                              }
                                          }
      
                                          // Column 1
                                          TableViewColumn {
                                              role: "role_m_one";
                                              width: 70
      
                                              delegate:
                                              Button {
                                                  anchors.verticalCenter: parent.verticalCenter
                                                  text: "Click Me!"
                                                  width: parent.width
      
                                                  onClicked: {
                                                      console.log(styleData.value)
                                                  }
                                              }
                                          }
      
      
                                          // Column 2
                                          TableViewColumn {
                                              title: "Some Text";
                                              role: "role_m_two";
                                              width: 70
      
                                              delegate:
                                              TextField {
                                                  anchors.fill: parent
                                                  text: styleData.value
      
                                                  onTextChanged: {
      
                                                      // Print Model
                                                      theModel.print_rows()
                                                  }
                                              }
                                          }
                                      }
      
      

      main.cpp:

      
          MyModel model;
          engine.rootContext()->setContextProperty("theModel", &model);
      
      

      mymodel.h:

      #ifndef MYMODEL_H
      #define MYMODEL_H
      
      
      #include <QStandardItemModel>
      
      #include <QObject>
      #include <QAbstractTableModel>
      #include <QList>
      #include <QString>
      #include <QDebug>
      
      #include <QAbstractTableModel>
      #include <QDebug>
      
      
      struct SimpleData {
          bool enable;
          QString m_one;
          QString m_two;
      
          SimpleData() {
              enable = true;
              m_one = "1";
              m_two = "2";
          }
      
          SimpleData(bool enable, const QString m_one, const QString m_two) :
          enable(enable), m_one(m_one), m_two(m_two) {}
      };
      
      
      class MyModel : public QAbstractTableModel
      {
          Q_OBJECT
      
      public:
          explicit MyModel(QObject *parent = 0);
      
          int rowCount(const QModelIndex &parent = QModelIndex()) const ;
          int columnCount(const QModelIndex &parent = QModelIndex()) const;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
          bool setData(const QModelIndex & index, const QVariant &value, int role = Qt::EditRole);
          QHash<int, QByteArray> roleNames() const;
      
          QList<SimpleData*> m_data_list;
      
          enum MyRoles {
              RoleOne = Qt::UserRole + 1,
              RoleTwo,
              RoleThree
          };
      
      signals:
      
      public slots:
          void print_rows();
      };
      
      #endif // MYMODEL_H
      
      

      mymodel.cpp:

      #include "mymodel.h"
      
      MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent) {
      
      //    SimpleData *m_simpledata;
      
      //    // Create junk model data
      //    m_simpledata = new SimpleData(true, "dfsdfs", "sdsds");
      //    m_data_list.append(m_simpledata);
      //    m_simpledata = new SimpleData(false, "asd2", "bsd2");
      //    m_data_list.append(m_simpledata);
      }
      
      int MyModel::rowCount(const QModelIndex & /*parent*/) const {
         return m_data_list.length();
      }
      
      int MyModel::columnCount(const QModelIndex & /*parent*/) const {
          return 3;
      }
      
      QVariant MyModel::data(const QModelIndex &index, int role) const {
      
          switch(role)
          {
              case RoleOne:
                  return m_data_list.at(index.row())->enable;
              case RoleTwo:
                  return m_data_list.at(index.row())->m_one;
              case RoleThree:
                  return m_data_list.at(index.row())->m_two;
          }
      
          return QVariant();
      }
      
      bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role) {
      
          qDebug() << "setData() called with:" << value;
      
          switch(role)
          {
              case RoleOne:
                  m_data_list[index.row()]->enable = value.toBool();
              case RoleTwo:
                  m_data_list[index.row()]->m_one = value.toString();
              case RoleThree:
                  m_data_list[index.row()]->m_two = value.toString();
          }
      
          return true;
      }
      
      QHash<int, QByteArray> MyModel::roleNames() const {
      
          QHash<int, QByteArray> roles;
          roles[RoleOne] = "role_enable";
          roles[RoleTwo] = "role_m_one";
          roles[RoleThree] = "role_m_two";
          return roles;
      }
      
      void MyModel::print_rows() {
      
          qDebug() << "Model Data:";
      
          for (int i=0; i < m_data_list.length(); i++) {
              qDebug() << "At" << i << ":" << m_data_list.at(i)->enable << m_data_list.at(i)->m_one << m_data_list.at(i)->m_two;
          }
      
      }
      
      

      myxml.cpp:

      
      void MyXML::readXMLFile()
      {
      
          QFile file(newFileName);
          if (!file.open(QFile::ReadOnly | QFile::Text))
          {
              //qDebug() << "Error: Cannot read file ";
              return;
          }
          QXmlStreamReader reader(file.readAll());
          file.close();
      
          while(!reader.atEnd()) {
              reader.readNext();
              if (reader.isStartElement()) {
                  ....
      			
      			....
      
                  foreach(const QXmlStreamAttribute &attr, reader.attributes()){
      
                      MyModel model;
                      SimpleData *m_simpledata;
                      
                      m_simpledata = new SimpleData(true, "for test", "test");
                      model.m_data_list.append(m_simpledata);
                   }
                  }
              }
          }
      }
      
      
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post