Export QTableView to PDF with a push button
-
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 ? -
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 ?@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. -
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 ?
-
@imene
You are trying to callQPrinter::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. -
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"); }
-
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"); }
@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.
-
@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!