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



  • 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);
                 }
                }
            }
        }
    }
    
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.