[SOLVED]How to write the model data to XML file ?



  • I have a QTableView set to a QStandardItemModel which if of 32*32 and each cell in model is a combobox. The combobox does not have fixed items .The Items (which is of QString ) count in the combobox can vary from 0 to 30-40. now i want to write the these cell data to XML file .
    How to write the model data to the XML file ?



  • If you simply want to write out data you could make use of the QXmlStreamReader class. Just pack your data in blocks line by line.

    Edit: of course QXmlStreamWriter*



  • @NetZwerg
    Thanks for the reply.
    I have used QXmlStreamWriter as shown below.
    But i had fixed 4 values . Now the value is constantly changing .

        //Open a new XML File
        file =new QFile(filename);
        file->open(QIODevice::WriteOnly);
    
        xmlWriter =new QXmlStreamWriter(file);
        xmlWriter->setAutoFormatting(true);
        xmlWriter->writeStartDocument();
    
    void XMLWriter::WriteFilterSettings(int filter_values[32][32])
    {
        QString var;
        xmlWriter->writeComment("comment");
        xmlWriter->writeStartElement("startelement");
    
        for (int row = 0; row < 32; ++row)
        {
            xmlWriter->writeStartElement(QString("RT%1").arg(row));
            for (int column = 0; column < 32; ++column)
            {
                if(filter_values[row][column] == 0)
                {
                    var ="TR";
                }
                else if(filter_values[row][column] == 1)
                {
                    var ="T";
                }
                else if(filter_values[row][column] == 2)
                {
                    var ="R";
                }
                else    //Disabled//
                {
                    var ="Disabled";
                }
                xmlWriter->writeTextElement(QString("SA%1").arg(column), var);
            }
            xmlWriter->writeEndElement();
        }
        xmlWriter->writeEndElement();
    }


  • The code seems fine, if I understand you correctly, your are now having real values and not having your "var" with fixed strings. Then just put the correct values of your model into the writer:

    model->item(row,column)->text();  //If you have text or data(Qt::DisplayRole) or similar


  • @NetZwerg
    I have set the values previously.

     if(var == "TR")
                {
                    filter_values[row][column] = 0;
                }

Log in to reply
 

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