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. [SOLVED]How to write the model data to XML file ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
xmlwrite
5 Posts 2 Posters 3.4k 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.
  • R Offline
    R Offline
    Ratzz
    wrote on 23 Jul 2015, 05:11 last edited by Ratzz
    #1

    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 ?

    --Alles ist gut.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NetZwerg
      wrote on 23 Jul 2015, 05:22 last edited by NetZwerg
      #2

      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*

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Ratzz
        wrote on 23 Jul 2015, 05:28 last edited by Ratzz
        #3

        @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();
        }

        --Alles ist gut.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NetZwerg
          wrote on 23 Jul 2015, 05:40 last edited by
          #4

          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
          
          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ratzz
            wrote on 23 Jul 2015, 06:13 last edited by
            #5

            @NetZwerg
            I have set the values previously.

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

            --Alles ist gut.

            1 Reply Last reply
            0

            5/5

            23 Jul 2015, 06:13

            • 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