Export a QTableWidget in PDF



  • hi!

    I should export the data of a QTableWidget in PDF.
    I do not print the widget, I have to export all the data.
    do you have any suggestions?



  • One way is to use the QPdfWriter class, here
    I guess you must daw onto the pages yourself with this.

    Might be easier to use a QPrinter with the output format set to QPrinter::PdfFormat.
    Then there is QTextDocument which you could use to format your data and then print that to a pdf.
    I am sure other people will suggest other things.



  • ok, I tried with this:

                QPrinter printer(QPrinter::PrinterResolution);
                printer.setOutputFormat(QPrinter::PdfFormat);
                printer.setPaperSize(QPrinter::A4);
                printer.setOrientation(QPrinter::Landscape);
                printer.setOutputFileName(strFile);
    
                QTextDocument doc;
                QString style("<style>");
                style.append("table { border-collapse: collapse;font-size:10px; }");
                style.append("table, th, td { border: 1px solid black;text-align: left; }");
                //style.append("th, td { border: 1px solid black;text-align: left; }");
                style.append("</style>");
    
                QString text("<table><thead>");
                text.append("<tr>");
                for (int i = 0; i < tbl->columnCount(); i++) {
                    text.append("<th>").append(tbl->horizontalHeaderItem(i)->data(Qt::DisplayRole).toString()).append("</th>");
                }
                text.append("</tr></thead>");
                text.append("<tbody>");
                for (int i = 0; i < tbl->rowCount(); i++) {
                    text.append("<tr>");
                    for (int j = 0; j < tbl->columnCount(); j++) {
                        QTableWidgetItem *item = tbl->item(i, j);
                        if (!item || item->text().isEmpty()) {
                            tbl->setItem(i, j, new QTableWidgetItem("0"));
                        }
                        text.append("<td>").append(tbl->item(i, j)->text()).append("</td>");
                    }
                    text.append("</tr>");
                }
                text.append("</tbody></table>");
                doc.setDefaultStyleSheet(style);
                doc.setHtml(text);
                doc.setPageSize(printer.pageRect().size());
                doc.print(&printer);
    

    the table in the pdf is created, but the style is not applied!



  • @fermatqt

    what about QTextTable ?



  • @the_ said in Export a QTableWidget in PDF:

    @fermatqt

    what about QTextTable ?

    ok I'm trying with QTextTable:

    const int columns = tbl->columnCount();
                const int rows = tbl->rowCount();
                QTextDocument doc;
                QTextCursor cursor(&doc);
                QTextTableFormat tableFormat;
                tableFormat.setHeaderRowCount(1);
                tableFormat.setAlignment(Qt::AlignHCenter);
                tableFormat.setCellPadding(0);
                tableFormat.setCellSpacing(0);
                tableFormat.setBorder(1);
                tableFormat.setBorderBrush(QBrush(Qt::SolidPattern));
                tableFormat.clearColumnWidthConstraints();
                QTextTable *textTable = cursor.insertTable(rows + 1, columns, tableFormat);
                QTextCharFormat tableHeaderFormat;
                tableHeaderFormat.setBackground(QColor("#DADADA"));
                for (int i = 0; i < columns; i++) {
                    QTextTableCell cell = textTable->cellAt(0, i);
                    cell.setFormat(tableHeaderFormat);
                    QTextCursor cellCursor = cell.firstCursorPosition();
                    cellCursor.insertText(tbl->horizontalHeaderItem(i)->data(Qt::DisplayRole).toString());
                }
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < columns; j++) {
                        QTableWidgetItem *item = tbl->item(i, j);
                        if (!item || item->text().isEmpty()) {
                            tbl->setItem(i, j, new QTableWidgetItem("0"));
                        }
    
                        QTextTableCell cell = textTable->cellAt(i, j);
                        QTextCursor cellCursor = cell.firstCursorPosition();
                        cellCursor.insertText(tbl->item(i, j)->text());
                    }
                }
                cursor.movePosition(QTextCursor::End);
                QPrinter printer(QPrinter::PrinterResolution);
                printer.setOutputFormat(QPrinter::PdfFormat);
                printer.setPaperSize(QPrinter::A4);
                printer.setOrientation(QPrinter::Landscape);
                printer.setOutputFileName(strFile);
                doc.setDocumentMargin(0);
                doc.setTextWidth(5);
                doc.print(&printer);
    

    the only problem I have is that I do not see the table header.
    It is overwritten by the first row of data set.



  • ok, I did in this way and works:

    QTextTableCell cell = textTable->cellAt(i + 1, j);
    

    thanks a lot!!!



  • Glad you were able to get it working :-)

    Maybe you could set your post to SOLVED now so other people will find the solution.


Log in to reply
 

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