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. Writing QTableWidget data in a XML file?
Forum Updated to NodeBB v4.3 + New Features

Writing QTableWidget data in a XML file?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 6.0k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    itsmemax
    wrote on last edited by
    #1

    Hi. I need to save this table:
    !http://i.imgur.com/M0vu5r1.png(http://i.imgur.com/M0vu5r1.png)!

    in a XML file, which will look like this:
    @<?xml version="1.0" encoding="UTF-8"?>

    <UIS_CONFIG>

    <CONFIG>
        <NR_OF_UIS>1</NR_OF_UIS>
    </CONFIG>
    
    <DIGIO>
        <IO>
            <PIN>PINName 1</PIN>
            <VAL>Angeschlossene Taste 1</VAL>
        </IO>
        <IO>
            <PIN>PINName 2</PIN>
            <VAL>Angeschlossene Taste 2</VAL>
        </IO>
        <IO>
            <PIN>PINName 3</PIN>
            <VAL>Angeschlossene Taste 3</VAL>
        </IO>
        <IO>
            <PIN>PINName 4</PIN>
            <VAL>Angeschlossene Taste 4</VAL>
        </IO>
    </DIGIO>
    

    </UIS_CONFIG>@

    Here's my code:
    @void MainWindow::on_actionExport_triggered()
    {
    QString filename = QFileDialog::getSaveFileName(
    this,
    tr("Konfig speichern"),
    QDir::currentPath(),
    tr("Konfigurationsdatei (*.xml)") );

        if( !filename.isNull()){
            QFile::remove(filename);
            QFile file(filename + ".xml"&#41;;
            file.open(QIODevice::WriteOnly | QIODevice::Text&#41;;
            QTextStream outPut(&file&#41;;
            outPut << "Test";
    
    
        }
    

    }
    @

    You see, it just writes an XML file at a optional location with the content "Test". I really have got not plan how to put the table as a XML file. Help please?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      You will need to traverse the data of the tablewidget by yourself (e.g. over it's model).
      Then you could use QDomDocument and its methods in coin the way you already did:
      @
      void MainWindow::on_actionExport_triggered()
      {
      .... //the code you already have

       outPut << myDomDocument->toString(4); //write XML string with an indentation of 4 spaces
      

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • I Offline
        I Offline
        itsmemax
        wrote on last edited by
        #3

        I really don't understand this thing. http://qt-project.org/doc/qt-4.8/qdomdocument.html
        I still don't know how to put (for example) row 2, column 2 at this point:
        <VAL>Angeschlossene Taste 2</VAL>
        .. Also the help in QT Creator itself does not help me..

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          for example:
          @
          QDomDocument dd;

          QDomElement digioElement = dd.createElement("digio");
          //add digioElement to it's parent element (UIS_CONFIG)...

          for( int r = 0; r < table->rowCount(); ++r )
          {
          QDomElement ioElement = dd.createElement("io");
          digioElement.appendChild(ioElement);

           QDomElement pinElement = dd.createElement("pin");
                 pinElement.appendChild( dd.createTextNode(table->item(r,0)->data(Qt::DisplayRole).toString() );
           ioElement.appendChild(pinElement);
          
           QDomElement valElement = dd.createElement("val");
                 valElement.appendChild( dd.createTextNode( table->item(r,2)->data(Qt::DisplayRole).toString() );
           ioElement.appendChild(valElement);
          

          }
          outPut << dd.toString(4);
          @

          You will need to add the UIS_CONFIG and CONFIG element ... i left that out.
          It should give you an idea how to further proceed.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • I Offline
            I Offline
            itsmemax
            wrote on last edited by
            #5

            Nice, thank you. I'll try it & see if I can do it.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              itsmemax
              wrote on last edited by
              #6

              I've tired your code for testing reasons, just to see how it works. Sadly it didn't work. It created the file, but the file was empty. So I've tried a qDebug, but the String is definitely empty..

              @void MainWindow::on_actionExport_triggered()
              {
              QString filename = QFileDialog::getSaveFileName(
              this,
              tr("Konfig speichern"),
              QDir::currentPath(),
              tr("Konfigurationsdatei (*.xml)") );

                  if( !filename.isNull()){
                      QFile::remove(filename);
                      QFile file&#40;filename + ".xml"&#41;;
                      file.open(QIODevice::WriteOnly | QIODevice::Text);
                      QTextStream outPut(&file);
              
                      //QDomDocument
              
                      QDomDocument xmlDocument;
              
                      QDomElement digioElement = xmlDocument.createElement("digio");
              
                      for( int r = 0; r < ui->tableWidget_5->rowCount(); ++r )
                      {
                           QDomElement ioElement = xmlDocument.createElement("io");
                           digioElement.appendChild(ioElement);
              
                           QDomElement pinElement = xmlDocument.createElement("pin");
                                 pinElement.appendChild( xmlDocument.createTextNode(ui->tableWidget_5->item(r,0)->data(Qt::DisplayRole).toString() ));
                           ioElement.appendChild(pinElement);
              
                           QDomElement valElement = xmlDocument.createElement("val");
                                 valElement.appendChild( xmlDocument.createTextNode(ui->tableWidget_5->item(r,1)->data(Qt::DisplayRole).toString() ));
                           ioElement.appendChild(valElement);
                      }
                      qDebug() <&lt; xmlDocument.toString(4);
              
              
              
              
                  }
              

              }@

              Why?

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                because digioElement is never added to the document-object.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  itsmemax
                  wrote on last edited by
                  #8

                  Ahh, thanks! Now I've understood it:
                  @void MainWindow::on_actionExport_triggered()
                  {
                  QString filename = QFileDialog::getSaveFileName(
                  this,
                  tr("Konfig speichern"),
                  QDir::currentPath(),
                  tr("Konfigurationsdatei (*.xml)") );

                      if( !filename.isNull()){
                          QFile::remove(filename);
                          QFile file&#40;filename + ".xml"&#41;;
                          file.open(QIODevice::WriteOnly | QIODevice::Text);
                          QTextStream outPut(&file);
                  
                          //QDomDocument
                  
                          QDomDocument xmlDocument;
                  
                          QDomElement digioElement = xmlDocument.createElement("DIGIO");
                  
                          for( int r = 0; r < ui->tableWidget_5->rowCount(); ++r )
                          {
                               QDomElement ioElement = xmlDocument.createElement("IO");
                               digioElement.appendChild(ioElement);
                  
                               QDomElement pinElement = xmlDocument.createElement("PIN");
                                     pinElement.appendChild( xmlDocument.createTextNode(ui->tableWidget_5->item(r,0)->data(Qt::DisplayRole).toString() ));
                               ioElement.appendChild(pinElement);
                  
                               QDomElement valElement = xmlDocument.createElement("VAL");
                                     valElement.appendChild( xmlDocument.createTextNode(ui->tableWidget_5->item(r,1)->data(Qt::DisplayRole).toString() ));
                               ioElement.appendChild(valElement);
                  
                               xmlDocument.appendChild(digioElement);
                          }
                  
                  
                          outPut <&lt; xmlDocument.toString(4);
                  
                  
                  
                  
                      }
                  

                  }@

                  Works like a charm. I see, if I can put the other parts of the XML file now my self in there ;)

                  1 Reply Last reply
                  0
                  • raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    yea...but don't add it in the loop ;)
                    You do not need to add it on every iteration ... just add it to it's parent right after creation.
                    I haven't added it to to the document because there are missing elements as i wrote. Because i left out to create and add UIS_CONFIG and CONFIG elements...

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    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