Writing QTableWidget data in a XML file?



  • 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?


  • Moderators

    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
    

    }
    @



  • 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..


  • Moderators

    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.



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



  • 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?


  • Moderators

    because digioElement is never added to the document-object.



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


  • Moderators

    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...


Log in to reply
 

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