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. QXmlStream and QTableWidget read and write xml file
Forum Updated to NodeBB v4.3 + New Features

QXmlStream and QTableWidget read and write xml file

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 3.6k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #8
    1. QXmlStreamWriter can operate directly on a QIODevice (like QFile), no need to put a string in the middle and use QTextStream. Your code crates a big inconsistency with encodings if you use writeStartDocument
    2. when i add another records last one is empty

    How do you add them?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    behruz montazeriB 1 Reply Last reply
    0
    • VRoninV VRonin
      1. QXmlStreamWriter can operate directly on a QIODevice (like QFile), no need to put a string in the middle and use QTextStream. Your code crates a big inconsistency with encodings if you use writeStartDocument
      2. when i add another records last one is empty

      How do you add them?

      behruz montazeriB Offline
      behruz montazeriB Offline
      behruz montazeri
      wrote on last edited by behruz montazeri
      #9

      @VRonin

      When i run the program and add some record to QTableWidget the last one won't write to file.
      0_1536250415442_Scr.png

      <words>
          <row ID="0">
              <col1 ID="0" text="item 1"/>
              <col2 ID="1" text="item 11"/>
          </row>
          <row ID="1">
              <col1 ID="0" text="item 2 "/>
              <col2 ID="1" text="item 22"/>
          </row>
          <row ID="2">
              <col1 ID="0" text="item 3"/>
              <col2 ID="1" text="item 33"/>
          </row>
          <row ID="3">
              <col1 ID="0" text="a"/>
              <col2 ID="1" text="b"/>
          </row>
          <row ID="4">
              <col1 ID="0" text="c"/>
              <col2 ID="1" text="d"/>
          </row>
          <row ID="5">
              <col1 ID="0" text="e"/>
              <col2 ID="1" text=""/>
          </row>
      
      1 Reply Last reply
      0
      • behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by
        #10

        I want the reverse functionality to read the file and add to QTableWidget i tried but i couln't :

            QFile file( "/home/behruz/Qt/TestPro/my.xml" );
            if( !file.open( QFile::ReadOnly | QFile::Text ) ) {
                // print error cannot open
            }
            QXmlStreamReader reader;
            reader.setDevice( &file );
            reader.readNext();
        
            if (reader.readNextStartElement()) {
                if (reader.name() == "words"){
                    while(reader.readNextStartElement()){
                        if(reader.name() == "col1"){
                            QString s = reader.readElementText();
                            qDebug(qPrintable(s));
                        }
                    }
                }
            }
        
        JonBJ 1 Reply Last reply
        0
        • behruz montazeriB behruz montazeri

          I want the reverse functionality to read the file and add to QTableWidget i tried but i couln't :

              QFile file( "/home/behruz/Qt/TestPro/my.xml" );
              if( !file.open( QFile::ReadOnly | QFile::Text ) ) {
                  // print error cannot open
              }
              QXmlStreamReader reader;
              reader.setDevice( &file );
              reader.readNext();
          
              if (reader.readNextStartElement()) {
                  if (reader.name() == "words"){
                      while(reader.readNextStartElement()){
                          if(reader.name() == "col1"){
                              QString s = reader.readElementText();
                              qDebug(qPrintable(s));
                          }
                      }
                  }
              }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #11

          @behruz-montazeri
          Compare your code here line for line against your code above for outputting the XML.

          writer.writeAttribute(QStringLiteral("text"),ui->tableWidget->model()->index(i,j).data().toString());
          

          shows you are storing text as an attribute (text="item1").

          That does not pair with your

          QString s = reader.readElementText();
          

          which will be the text inside each element, where you store nothing....

          behruz montazeriB 1 Reply Last reply
          0
          • JonBJ JonB

            @behruz-montazeri
            Compare your code here line for line against your code above for outputting the XML.

            writer.writeAttribute(QStringLiteral("text"),ui->tableWidget->model()->index(i,j).data().toString());
            

            shows you are storing text as an attribute (text="item1").

            That does not pair with your

            QString s = reader.readElementText();
            

            which will be the text inside each element, where you store nothing....

            behruz montazeriB Offline
            behruz montazeriB Offline
            behruz montazeri
            wrote on last edited by behruz montazeri
            #12
                QFile file("/home/behruz/Qt/TestPro/my.xml");
                if (file.open(QIODevice::ReadOnly)) {
                    QXmlStreamReader reader(file.readAll());
                    file.close();
                    while(!reader.atEnd()) {
                        reader.readNext();
                        if (reader.isStartElement()) {
                            if (reader.name() == "words") {
                                reader.readNextStartElement();
                                if(reader.name() == "row"){
                                    reader.readNextStartElement();
            
                                    foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                                        if (attr.name().toString() == QLatin1String("text")) {
                                            QString attribute_value = attr.value().toString();
                                            qDebug(qPrintable(attribute_value));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            

            It only shows the item 1 how populate it in QTableWidget ?

            JonBJ VRoninV 2 Replies Last reply
            0
            • behruz montazeriB behruz montazeri
                  QFile file("/home/behruz/Qt/TestPro/my.xml");
                  if (file.open(QIODevice::ReadOnly)) {
                      QXmlStreamReader reader(file.readAll());
                      file.close();
                      while(!reader.atEnd()) {
                          reader.readNext();
                          if (reader.isStartElement()) {
                              if (reader.name() == "words") {
                                  reader.readNextStartElement();
                                  if(reader.name() == "row"){
                                      reader.readNextStartElement();
              
                                      foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                                          if (attr.name().toString() == QLatin1String("text")) {
                                              QString attribute_value = attr.value().toString();
                                              qDebug(qPrintable(attribute_value));
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              

              It only shows the item 1 how populate it in QTableWidget ?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #13

              @behruz-montazeri
              Again, compare your reader against the writer code you have. That loops round every row, doesn't it? But your reader only has an if (reader.name() == "words"). Plus I don't see anything looping over the <col...> elements at all. And you say it only shows the first item (first row & column). Do you think that could be related to no loops?

              I'm not going to write it for you. You need to look up the docs and read the necessary elements in the necessary loops. You're headed in the right direction, just think about where your code needs to loop and read what to get back to what you had for the writing code.

              behruz montazeriB 1 Reply Last reply
              0
              • JonBJ JonB

                @behruz-montazeri
                Again, compare your reader against the writer code you have. That loops round every row, doesn't it? But your reader only has an if (reader.name() == "words"). Plus I don't see anything looping over the <col...> elements at all. And you say it only shows the first item (first row & column). Do you think that could be related to no loops?

                I'm not going to write it for you. You need to look up the docs and read the necessary elements in the necessary loops. You're headed in the right direction, just think about where your code needs to loop and read what to get back to what you had for the writing code.

                behruz montazeriB Offline
                behruz montazeriB Offline
                behruz montazeri
                wrote on last edited by
                #14

                @JonB
                What's the loop statement ? I know it needs a loop but i can't find where and how.

                JonBJ 1 Reply Last reply
                0
                • behruz montazeriB behruz montazeri

                  @JonB
                  What's the loop statement ? I know it needs a loop but i can't find where and how.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #15

                  @behruz-montazeri
                  Loop statements are for, while, foreach. The writer used fors because it knew how many rows & columns there were. You'll probably want whiles in the reader, certainly to move through all the <row> elements.

                  behruz montazeriB 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @behruz-montazeri
                    Loop statements are for, while, foreach. The writer used fors because it knew how many rows & columns there were. You'll probably want whiles in the reader, certainly to move through all the <row> elements.

                    behruz montazeriB Offline
                    behruz montazeriB Offline
                    behruz montazeri
                    wrote on last edited by
                    #16

                    @JonB
                    I already used a loop :

                    foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                                            if (attr.name().toString() == QLatin1String("text")) {
                                                stList.append( attr.value().toString());
                                                qDebug() << stList;
                                            }
                    

                    But it seems it only works for one record. Give me the code or a real hint. Thanks.

                    1 Reply Last reply
                    0
                    • behruz montazeriB behruz montazeri
                          QFile file("/home/behruz/Qt/TestPro/my.xml");
                          if (file.open(QIODevice::ReadOnly)) {
                              QXmlStreamReader reader(file.readAll());
                              file.close();
                              while(!reader.atEnd()) {
                                  reader.readNext();
                                  if (reader.isStartElement()) {
                                      if (reader.name() == "words") {
                                          reader.readNextStartElement();
                                          if(reader.name() == "row"){
                                              reader.readNextStartElement();
                      
                                              foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                                                  if (attr.name().toString() == QLatin1String("text")) {
                                                      QString attribute_value = attr.value().toString();
                                                      qDebug(qPrintable(attribute_value));
                                                  }
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      

                      It only shows the item 1 how populate it in QTableWidget ?

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #17

                      @behruz-montazeri said in QXmlStream and QTableWidget read and write xml file:

                      QXmlStreamReader reader(file.readAll());

                      ??? This makes no sense, it should be QXmlStreamReader reader(&file); I guess

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • behruz montazeriB Offline
                        behruz montazeriB Offline
                        behruz montazeri
                        wrote on last edited by behruz montazeri
                        #18

                        I corrected reading by using two loops.

                         QStringList stList;
                        
                            QFile file("/home/behruz/Desktop/Temp/QTableWidgetXml/my.xml");
                            if( !file.open( QFile::ReadOnly | QFile::Text ) ) {
                                // print error cannot open
                            }
                            QXmlStreamReader reader(&file);
                        
                            while(!reader.atEnd()) {
                                reader.readNext();
                                if (reader.isStartElement()) {
                                    reader.readNextStartElement();
                                    for(int i=0;i<ui->tableWidget->rowCount();i++){
                                        if(reader.name() == "row"){
                        
                                            reader.readNextStartElement();
                                            for(int i=0;i<2;i++){
                                                //                        qDebug() << reader.name();
                                                foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
                        
                                                    if (attr.name().toString() == QLatin1String("text")) {
                        
                                                        stList.append( attr.value().toString());
                                                        qDebug() << stList;
                                                    }
                        
                        
                                                }
                                                reader.readNextStartElement();
                                                reader.readNextStartElement();
                                            }
                                        }
                                        reader.readNextStartElement();
                                    }
                        
                                }
                            }
                        
                        
                                for(int i=0, maxRow = ui->tableWidget->rowCount();i<maxRow ;++i){
                                    for(int j=0, maxCol = ui->tableWidget->columnCount();j<maxCol ;++j){
                                        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg( stList[L]));
                                        qDebug() << stList[i];
                                        ui->tableWidget->setItem(i, j, newItem);
                                       L++;
                                    }
                                }
                        
                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #19

                          maybe the XmlModelSerialiserPrivate::writeXmlElement and XmlModelSerialiserPrivate::readXmlElement methods from https://github.com/VSRonin/QtModelUtilities/blob/master/src/xmlmodelserialiser.cpp might help

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          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