[SOLVED] Loop tableWidget export



  • Hopefully my last question for this case. I've got four QTableWidgets here. All of them contain 64 rows and 3 columns. I want to export all of them into a single XML file which should look like this:
    @<UIS_CONFIG>
    <CONFIG>
    <NR_OF_UIS>64</NR_OF_UIS>
    </CONFIG>
    <DIGIO>
    <IO>
    <PIN>1</PIN>
    <VAL>0</VAL>
    </IO>
    <IO>
    <PIN>2</PIN>
    <VAL>0</VAL>
    </IO>
    <IO>
    <PIN>3</PIN>
    <VAL>0</VAL>
    </IO>
    <IO>
    <PIN>4</PIN>
    <VAL>0</VAL>
    </IO>
    ..............
    </DIGIO>
    </UIS_CONFIG>@

    "PIN" is the PinNr., "VAL" is the connected key (column 3 of my table). Here's a picture of the table (the other three are exactly the same):
    !http://i.imgur.com/M0vu5r1.png(table img)!

    Now, everything's fine. I can successfully export one of the four tables. But now I try to loop the whole thing, to export the four tables at once. Here's what I've tried:
    @void MainWindow::on_actionExport_triggered()
    {
    QString filename = QFileDialog::getSaveFileName(
    this,
    tr("Konfig speichern"),
    QDir::currentPath(),
    tr("Konfigurationsdatei (*.xml)") );

        if( !filename.isNull()) {
            QFile::remove(filename);
            if ( !filename.endsWith(".xml", Qt::CaseInsensitive))
                filename += ".xml";
            QFile file&#40;filename&#41;;
            file.open(QIODevice::WriteOnly | QIODevice::Text&#41;;
            QTextStream outPut(&file);
    
            //QDomDocument
    
            QDomDocument xmlDocument;
            QString numberOfRows = QString::number(ui->tableWidget_5->rowCount());
    
            QDomElement NOUElement = xmlDocument.createElement("NR_OF_UIS");
            NOUElement.appendChild( xmlDocument.createTextNode(numberOfRows));
    
            QDomElement configElement = xmlDocument.createElement("CONFIG");
            QDomElement digioElement = xmlDocument.createElement("DIGIO");
    
            QDomElement uisConfigElement = xmlDocument.createElement("UIS_CONFIG");
            configElement.appendChild(NOUElement);
            uisConfigElement.appendChild(configElement);
            uisConfigElement.appendChild(digioElement);
    
            QDomNode tmpNode;
            QDomElement tmpElement;
            QDomText tmpTxt;
            QDomText tmpTxt2;
    
            QTableWidget * pTable;
    
            for ( int t = 0; t < 4; ++t)
            {
    
                if ( t = 0 ) {
                    pTable = ui->tableWidget_5;}
                if ( t = 1 ) {
                    pTable = ui->tableWidget_6;}
                if ( t = 2 ) {
                    pTable = ui->tableWidget_7;}
                if ( t = 3 ) {
                    pTable = ui->tableWidget_8;}
    
    
                for( int r = 0; r < pTable->rowCount(); ++r )
                {
    
                     QDomElement ioElement = xmlDocument.createElement("IO");
                     tmpNode = digioElement.appendChild(ioElement);
                     QDomNode nodePin = tmpNode.appendChild(xmlDocument.createElement("PIN"));
                     QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL"));
    
                     tmpTxt =  xmlDocument.createTextNode(pTable->item(r,0)->data(Qt::DisplayRole).toString());
                     nodePin.appendChild(tmpTxt);
    
                     QComboBox *combo = qobject_cast<QComboBox*>(pTable->cellWidget(r,2));
                     if(combo)
                     {
                         QString sIndex;
                         sIndex = QString("%1").arg(combo->currentIndex());
                         tmpTxt =  xmlDocument.createTextNode(sIndex);
                         qDebug() << sIndex;
                     }
    
                     nodePin.appendChild(tmpTxt);
                     nodeVal.appendChild(tmpTxt);
                }
            }
            xmlDocument.appendChild(uisConfigElement);
    
            outPut <&lt; xmlDocument.toString(4);
    
    
    
    
        }
    

    }@

    So, here I try to loop this thing...:
    @for ( int t = 0; t < 4; ++t)
    {

                if ( t = 0 ) {
                    pTable = ui->tableWidget_5;}
                if ( t = 1 ) {
                    pTable = ui->tableWidget_6;}
                if ( t = 2 ) {
                    pTable = ui->tableWidget_7;}
                if ( t = 3 ) {
                    pTable = ui->tableWidget_8;}
    

    ...@

    But this does not seem to work. What can I try?


  • Moderators

    do you really mean "t = 0" in your if statements? ;)



  • I think so, yes. :-S Because I named the int in my for-loop "t"...


  • Moderators

    reaaaally?! or do you mean "t == 0"?



  • F*ck. You're right. My stupidness, sorry! Thanks..



  • Ah; i forgot.. When the application exports the second table, it starts again at 1, not at 65. Sure.. But how can I start there at 65, and at the third table with 129, and at the fourth with 193? I thought of something like this:

    @ for ( int t = 0; t < 4; ++t)
    {
    if ( t == 0 ) {
    pTable = ui->tableWidget_5;}
    int x = r + (t64);
    if ( t == 1 ) {
    pTable = ui->tableWidget_6;}
    int x = r + (t
    64);
    if ( t == 2 ) {
    pTable = ui->tableWidget_7;}
    int x = r + (t64);
    if ( t == 3 ) {
    pTable = ui->tableWidget_8;}
    int x = r + (t
    64);

                for( int r = 0; r < pTable->rowCount(); ++r )
                {
    
                     QDomElement ioElement = xmlDocument.createElement("IO");
                     tmpNode = digioElement.appendChild(ioElement);
                     QDomNode nodePin = tmpNode.appendChild(xmlDocument.createElement("PIN"));
                     QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL"));
    
                     int x = r + (t*64);
                     
                     tmpTxt =  xmlDocument.createTextNode(pTable->item(r,0)->data(Qt::DisplayRole).toString());
                     nodePin.appendChild(tmpTxt);
    
                     QComboBox *combo = qobject_cast<QComboBox*>(pTable->cellWidget(r,2));
                     if(combo)
                     {
                         QString sIndex;
                         sIndex = QString("%1").arg(combo->currentIndex());
                         tmpTxt =  xmlDocument.createTextNode(sIndex);
                         qDebug() << sIndex;
                     }
    
                     nodePin.appendChild(tmpTxt);
                     nodeVal.appendChild(tmpTxt);
                }
            }@
    

    So, to multi the integer "t" with 64:
    @int x = r + (t*64);@

    But at this moment the code does not know "r". So this is not possible.. Any ideas?


  • Moderators

    then create an member which stores the start value "globally" outside the loop.



  • Which start value? r or t? The rest of your statement is clear, good idea :P



  • I've tried it now like this:
    @void MainWindow::on_actionExport_triggered()
    {
    QString filename = QFileDialog::getSaveFileName(
    this,
    tr("Konfig speichern"),
    QDir::currentPath(),
    tr("Konfigurationsdatei (*.xml)") );

        if( !filename.isNull()) {
            QFile::remove(filename);
            if ( !filename.endsWith(".xml", Qt::CaseInsensitive))
                filename += ".xml";
            QFile file&#40;filename&#41;;
            file.open(QIODevice::WriteOnly | QIODevice::Text);
            QTextStream outPut(&file);
    
            //QDomDocument
    
            QDomDocument xmlDocument;
            QString numberOfRows = QString::number(ui->table1->rowCount());
    
            QDomElement NOUElement = xmlDocument.createElement("NR_OF_UIS");
            NOUElement.appendChild( xmlDocument.createTextNode(numberOfRows));
    
            QDomElement configElement = xmlDocument.createElement("CONFIG");
            QDomElement digioElement = xmlDocument.createElement("DIGIO");
    
            QDomElement uisConfigElement = xmlDocument.createElement("UIS_CONFIG");
            configElement.appendChild(NOUElement);
            uisConfigElement.appendChild(configElement);
            uisConfigElement.appendChild(digioElement);
    
            QDomNode tmpNode;
            QDomText tmpTxt;
    
            QTableWidget * pTable;
    
            int t = 0;
            int r = 0;
            int x = 0;
            QString xString = QString::number(x);
    
    
            for ( t = 0; t < 4; ++t)
            {
                if ( t == 0 ) {
                    pTable = ui->table1;}
                    x = r + (t*64);
                    xString = QString::number(x);
                if ( t == 1 ) {
                    pTable = ui->table2;}
                    x = r + (t*64);
                    xString = QString::number(x);
                if ( t == 2 ) {
                    pTable = ui->table3;}
                    x = r + (t*64);
                    xString = QString::number(x);
                if ( t == 3 ) {
                    pTable = ui->table4;}
                    x = r + (t*64);
                    xString = QString::number(x);
    
    
                for( r = 0; r < pTable->rowCount(); ++r )
                {
    
    
    
                     QDomElement ioElement = xmlDocument.createElement("IO");
                     tmpNode = digioElement.appendChild(ioElement);
                     QDomNode nodePin = tmpNode.appendChild(xmlDocument.createElement("PIN"));
                     QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL"));
    
                     tmpTxt =  xmlDocument.createTextNode(xString);
                     nodePin.appendChild(tmpTxt);
    
                     QComboBox *combo = qobject_cast<QComboBox*>(pTable->cellWidget(r,2));
                     if(combo)
                     {
                         QString sIndex;
                         sIndex = QString("%1").arg(combo->currentIndex());
                         tmpTxt =  xmlDocument.createTextNode(sIndex);
                         qDebug() << sIndex;
                     }
    
                     nodePin.appendChild(tmpTxt);
                     nodeVal.appendChild(tmpTxt);
                }
            }
            xmlDocument.appendChild(uisConfigElement);
    
            outPut <&lt; xmlDocument.toString(4);
    
    
    
    
        }
    

    }@

    So I've made t, r and x global. Then I've created a QString, named xString. xString I give the value of x, everytime when x is set new. Then I try to give tmpText (which will be the text of the element "PIN" in the XML file) the value of xString.. But this does not seem to work at all. All PIN elements in the XML file are 0. Why?



  • It's solved!!

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

        if( !filename.isNull()) {
            QFile::remove(filename);
            if ( !filename.endsWith(".xml", Qt::CaseInsensitive))
                filename += ".xml";
            QFile file&#40;filename&#41;;
            file.open(QIODevice::WriteOnly | QIODevice::Text);
            QTextStream outPut(&file);
    
            //QDomDocument
    
            QDomDocument xmlDocument;
            QString numberOfRows = QString::number(4);
    
            QDomElement NOUElement = xmlDocument.createElement("NR_OF_UIS");
            NOUElement.appendChild( xmlDocument.createTextNode(numberOfRows));
    
            QDomElement configElement = xmlDocument.createElement("CONFIG");
            QDomElement digioElement = xmlDocument.createElement("DIGIO");
    
            QDomElement uisConfigElement = xmlDocument.createElement("UIS_CONFIG");
            configElement.appendChild(NOUElement);
            uisConfigElement.appendChild(configElement);
            uisConfigElement.appendChild(digioElement);
    
            QDomNode tmpNode;
            QDomText tmpTxt;
    
            QTableWidget * pTable;
    
    
            int iPinNo = 0;
            QString sPinNo;
    
    
            for ( int t = 0; t < 4; ++t)
            {
    
                if ( t == 0 ) pTable = ui->table1;
                if ( t == 1 ) pTable = ui->table2;
                if ( t == 2 ) pTable = ui->table3;
                if ( t == 3 ) pTable = ui->table4;
    
    
    
                for( int r = 0; r < pTable->rowCount(); ++r )
                {
    
                    iPinNo = r + (t*64);
                    sPinNo = QString::number(iPinNo);
    
                     QDomElement ioElement = xmlDocument.createElement("IO");
                     tmpNode = digioElement.appendChild(ioElement);
                     QDomNode nodePin = tmpNode.appendChild(xmlDocument.createElement("PIN"));
                     QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL"));
    
                     tmpTxt =  xmlDocument.createTextNode(sPinNo);
                     nodePin.appendChild(tmpTxt);
    
                     QComboBox *combo = qobject_cast<QComboBox*>(pTable->cellWidget(r,2));
                     if(combo)
                     {
                         QString sIndex;
                         sIndex = QString("%1").arg(combo->currentIndex());
                         tmpTxt =  xmlDocument.createTextNode(sIndex);
                         qDebug() << sIndex;
                     }
    
                     nodePin.appendChild(tmpTxt);
                     nodeVal.appendChild(tmpTxt);
                }
            }
            xmlDocument.appendChild(uisConfigElement);
    
            outPut <&lt; xmlDocument.toString(4);
    
    
    
    
        }
    

    }@

    Thanks to raven-worx anyways! (:


Log in to reply
 

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