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 QTableView to PDF with a push button
QtWS25 Last Chance

Export QTableView to PDF with a push button

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 984 Views
  • 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.
  • I Offline
    I Offline
    imene
    wrote on last edited by imene
    #1
    void MainWindow::on_SaveReport_Button_clicked()
    {
        QString filename="users.pdf";
           //Parametres d'impression
           QPrinter printer(QPrinter::HighResolution);
           printer.setOutputFileName(filename);
           printer.setPageSize(QPrinter::A4);
           printer.setOutputFormat(QPrinter::PdfFormat);
    
           QPainter painter(&printer);
           painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
           m_ui->Table_CSV->render( &painter );
           painter.end();
    }
    

    How can i change the table size to make it bigger in pdf file ?
    Can i change the path where the file is saved ?

    JonBJ 1 Reply Last reply
    0
    • I imene
      void MainWindow::on_SaveReport_Button_clicked()
      {
          QString filename="users.pdf";
             //Parametres d'impression
             QPrinter printer(QPrinter::HighResolution);
             printer.setOutputFileName(filename);
             printer.setPageSize(QPrinter::A4);
             printer.setOutputFormat(QPrinter::PdfFormat);
      
             QPainter painter(&printer);
             painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
             m_ui->Table_CSV->render( &painter );
             painter.end();
      }
      

      How can i change the table size to make it bigger in pdf file ?
      Can i change the path where the file is saved ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @imene said in Export QTableView to PDF with a push button:

      Can i change the path where the file is saved ?

      Yes, change QString filename="users.pdf"; to whatever you want, or use QFileDialog::getSaveFileName() if you mean you want the user to choose at runtime.

      1 Reply Last reply
      1
      • I Offline
        I Offline
        imene
        wrote on last edited by imene
        #3

        i want to save pdf file in an other folder, and i want to change the size of the table in pdf file:

        printer.setPageSize(QPrinter::A4); 
        

        how to deal with this function ?

        985fb18e-201d-4ce1-ba77-7c2b4b1b580d-image.png

        JonBJ 1 Reply Last reply
        0
        • I imene

          i want to save pdf file in an other folder, and i want to change the size of the table in pdf file:

          printer.setPageSize(QPrinter::A4); 
          

          how to deal with this function ?

          985fb18e-201d-4ce1-ba77-7c2b4b1b580d-image.png

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @imene said in Export QTableView to PDF with a push button:

          i want to save pdf file in an other folder

          So do so.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imene
            wrote on last edited by
            #5

            How to set the size of pdf file ?
            4361c3f0-13f6-4c09-995e-d8078a76911b-image.png

            JonBJ 1 Reply Last reply
            0
            • I imene

              How to set the size of pdf file ?
              4361c3f0-13f6-4c09-995e-d8078a76911b-image.png

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @imene
              You are trying to call QPrinter::setPageSize() which uses bool QPagedPaintDevice::setPageSize(const QPageSize &pageSize) but you are looking at documentation for void QPageLayout::setPageSize(const QPageSize &pageSize, const QMarginsF &minMargins = QMarginsF(0, 0, 0, 0)) for some reason.

              1 Reply Last reply
              1
              • I Offline
                I Offline
                imene
                wrote on last edited by imene
                #7

                I used this code and it works !

                void MainWindow::on_SaveReport_Button_clicked()
                {
                    QPixmap pix(m_ui->Table_CSV->size());
                      QPainter painter(&pix);
                      m_ui->Table_CSV->render(&painter);
                      painter.end();
                      QPrinter printer(QPrinter::HighResolution);
                      printer.setOrientation(QPrinter::Landscape);
                      printer.setOutputFormat(QPrinter::PdfFormat);
                      printer.setPaperSize(QPrinter::A4);
                      printer.setOutputFileName("test.pdf"); // will be in build folder
                     
                      painter.begin(&printer);
                      double xscale = printer.pageRect().width() / double(pix.width());
                      double yscale = printer.pageRect().height() / double(pix.height());
                      double scale = qMin(xscale, yscale);
                      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                                        printer.paperRect().y() + printer.pageRect().height() / 2);
                      painter.scale(scale, scale);
                      painter.translate(-m_ui->Table_CSV->width() / 2, -m_ui->Table_CSV->height() / 2);
                      painter.drawPixmap(0, 0, pix);
                
                    //QTextDocument doc;
                    //doc.setHtml("htmlcontent");
                    //doc.drawContents(&painter);
                    painter.end();
                      QMessageBox::information(this,"Done","PDF is saved successfully");
                }
                

                23d4f9c3-ca25-4efc-b020-9cee0612a450-image.png
                2e1dcf64-95e2-4f4d-9094-cbfa0d50ce29-image.png

                JonBJ 1 Reply Last reply
                0
                • I imene

                  I used this code and it works !

                  void MainWindow::on_SaveReport_Button_clicked()
                  {
                      QPixmap pix(m_ui->Table_CSV->size());
                        QPainter painter(&pix);
                        m_ui->Table_CSV->render(&painter);
                        painter.end();
                        QPrinter printer(QPrinter::HighResolution);
                        printer.setOrientation(QPrinter::Landscape);
                        printer.setOutputFormat(QPrinter::PdfFormat);
                        printer.setPaperSize(QPrinter::A4);
                        printer.setOutputFileName("test.pdf"); // will be in build folder
                       
                        painter.begin(&printer);
                        double xscale = printer.pageRect().width() / double(pix.width());
                        double yscale = printer.pageRect().height() / double(pix.height());
                        double scale = qMin(xscale, yscale);
                        painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                                          printer.paperRect().y() + printer.pageRect().height() / 2);
                        painter.scale(scale, scale);
                        painter.translate(-m_ui->Table_CSV->width() / 2, -m_ui->Table_CSV->height() / 2);
                        painter.drawPixmap(0, 0, pix);
                  
                      //QTextDocument doc;
                      //doc.setHtml("htmlcontent");
                      //doc.drawContents(&painter);
                      painter.end();
                        QMessageBox::information(this,"Done","PDF is saved successfully");
                  }
                  

                  23d4f9c3-ca25-4efc-b020-9cee0612a450-image.png
                  2e1dcf64-95e2-4f4d-9094-cbfa0d50ce29-image.png

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @imene said in Export QTableView to PDF with a push button:

                  printer.setOutputFileName("test.pdf"); // will be in build folder

                  No, it won't. This is "coincidence" (maybe from Qt Creator). It will actually be in whatever the "current working directory" happens to be when a user runs your program, which you don't know/control.

                  Normally --- unless you want this behaviour and know what you are doing --- do not use relative pathnames like this. Think about where you want the file to be, and perhaps use Qt's QStandardPaths Class to specify that.

                  1 Reply Last reply
                  3
                  • I Offline
                    I Offline
                    imene
                    wrote on last edited by imene
                    #9

                    @JonB said in Export QTableView to PDF with a push button:

                    QString filename="users.pdf";

                    I fixe it:

                    auto filename = QFileDialog::getSaveFileName(this, "Save Report", QDir::rootPath(), "PDF File(*.pdf)");
                          printer.setOutputFileName(filename); 
                    

                    Please help:
                    I've an other problem when the interface is minimized it only take a capture of the table view in the interface not the all the table!

                    d2d051cf-1521-48cd-bebd-726baecaf2e4-image.png

                    81aa8667-63f3-4916-b2d6-b96055648937-image.png

                    1 Reply Last reply
                    0

                    • Login

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