Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

write in data to html table



  • good afternoon
    There is an html table
    I need to fill it with data
    You should end up with a table with 5 columns and 5 rows
    But in the end, everything is displayed in one column
    Help find error
    Thanks

    void f(){
        QStringList massoil[size];
    
        _html += QString("<tr><td width=\"100%\" colspan=\"6\"><b><u>Result</u></b></td></tr>");
            _html += QString("<table border=\"1\" border-color=\"white\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">");
              _html += QString("<tr></tr>");
                if ( cb_mass_oil->isChecked())
                {
                    for (int col = 0; col < size; ++col)
                        for (int row = 0; row < 5; ++row)
                        {
                            if(row < massoil[col].size())
                                _html += QString("<tr><td width=\"40%\" align=\"center\" border=\"1\">%0</td></tr>")
                                        .arg(massoil[col][row]);
                            else
                                _html += QString("<tr><td width=\"40%\" align=\"center\" border=\"1\">%0</td></tr>")
                                        .arg("");
                        }
                       _html.replace(QRegularExpression("<tr(\\s[^>]*)?>(\\s*<td(\\s[^>]*)?>\\s*</td>\\s*)*</tr>", QRegularExpression::MultilineOption), "");
                }


  • move the row loop outside the column loop and don't close your <tr> tag at every column.
    Well formatted html is a dialect of xml so it's usually cleaner to use QXmlStreamWriter to create html rather than manually piece together strings


  • Lifetime Qt Champion

    Hi,

    @fender said in write in data to html table:

    QString("<tr><td width="40%" align="center" border="1">%0</td></tr>")

    You are only adding rows with single cells in them.

    The logic would be:

        for each row:
            add `<tr>` 
            for each column:
                 add `<td>"whaterever"</td>`
            add`</tr>`
    

Log in to reply