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. Export a QTableWidget in PDF
Forum Updated to NodeBB v4.3 + New Features

Export a QTableWidget in PDF

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 9.1k Views 3 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.
  • F Offline
    F Offline
    fermatqt
    wrote on 11 Apr 2017, 07:28 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on 11 Apr 2017, 07:49 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fermatqt
        wrote on 11 Apr 2017, 09:17 last edited by
        #3

        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!

        T 1 Reply Last reply 11 Apr 2017, 10:47
        0
        • F fermatqt
          11 Apr 2017, 09:17

          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!

          T Offline
          T Offline
          the_
          wrote on 11 Apr 2017, 10:47 last edited by
          #4

          @fermatqt

          what about QTextTable ?

          -- No support in PM --

          F 1 Reply Last reply 11 Apr 2017, 12:47
          0
          • T the_
            11 Apr 2017, 10:47

            @fermatqt

            what about QTextTable ?

            F Offline
            F Offline
            fermatqt
            wrote on 11 Apr 2017, 12:47 last edited by
            #5

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

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fermatqt
              wrote on 11 Apr 2017, 12:53 last edited by
              #6

              ok, I did in this way and works:

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

              thanks a lot!!!

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kenchan
                wrote on 11 Apr 2017, 13:02 last edited by
                #7

                Glad you were able to get it working :-)

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

                1 Reply Last reply
                0

                1/7

                11 Apr 2017, 07:28

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved