Qt to save browse preview print the function of the sample code

  • 2020-06-01 10:34:02
  • OfStack

Qt provides such as text, images, HTML ways to the operation of the document, mainly use the QPrinter classes and class QPainter, USES QFileDialog document window, QPrintPreviewDialog preview window class and QPrintDialog print window class, Qt5 also provides QPdfWriter class to implement, to the operation of the pdf here does not include open pdf file, Qt offers no way to directly as a file browser open pdf file 1 samples, can use third party libraries to implement.

Here used a picture of the way to save, preview and print, in fact, three functions basically 1.

1. Save PDF

(1) save the contents of a control

Some input class controls can call the print() function directly, some display class controls can call the render() function directly, and some controls do not. The code is as follows:


void MainWindow::on_btnSave_clicked()
{
 QString fileName = QFileDialog::getSaveFileName(this, tr(" export PDF file "), QString(), "*.pdf");
 if (!fileName.isEmpty())
 {
 //  If the file suffix is empty, it is used by default .pdf
 if (QFileInfo(fileName).suffix().isEmpty())
 {
  fileName.append(".pdf");
 }
 QPrinter printer;
 //  Specify the output format as pdf
 printer.setOutputFormat(QPrinter::PdfFormat);
 printer.setOutputFileName(fileName);
 // ui->textEdit->print(&printer);
 ui->tableWidget->render(&printer);
 }
}

(2) save the contents of some controls

All the contents to be saved here need to be put in a container, such as QWidget, which can also be saved in the way above, and in the way of pictures below. The code is as follows:


void MainWindow::on_btnSave_clicked()
{
 QString fileName = QFileDialog::getSaveFileName(this, tr(" save PDF file "), QString(), "*.pdf");
 if (!fileName.isEmpty())
 {
 //  If the file suffix is empty, it is used by default .pdf
 if (QFileInfo(fileName).suffix().isEmpty())
 {
  fileName.append(".pdf");
 }
 QPrinter printerPixmap(QPrinter::HighResolution);
 // Customize the size of the paper, and everything you want to print is there stackedWidget on 
 printerPixmap.setPageSize(QPrinter::Custom);
 printerPixmap.setPaperSize(QSizeF(ui->stackedWidget->height(), ui->stackedWidget->width()), QPrinter::Point);
 // Set the paper size to A4 , comment out here, suggest custom paper   , otherwise there will be a lot of white space 
 //printerPixmap.setPageSize(QPrinter::A4);
 // Horizontal print 
 printerPixmap.setOrientation(QPrinter::Landscape);
 // Set the output format to pdf
 printerPixmap.setOutputFormat(QPrinter::PdfFormat);
 // Set the output path 
 printerPixmap.setOutputFileName(fileName);
 // Get an image of the interface 
 QPixmap pixmap = QPixmap::grabWidget(ui->stackedWidget, ui->stackedWidget->rect());
 QPainter painterPixmap;
 painterPixmap.begin(&printerPixmap);
 QRect rect = painterPixmap.viewport();
 int x = rect.width() / pixmap.width();
 int y = rect.height() / pixmap.height();
 // The image ( All the things to draw ) in pdf Scale up to scale 
 painterPixmap.scale(x, y);
 // drawing 
 painterPixmap.drawPixmap(0, 0, pixmap);
 painterPixmap.end();
 QMessageBox::information(this, tr(" generate PDF"), tr(" save PDF File successfully "), QMessageBox::Ok);
 }
}

2, browse

Qt does not provide a way to browse pdf, but it can be implemented by using the third party library Poppler. Here are the relevant files: official website, compiled library files, and file packages of all files. pdf reader is implemented. You can compile libraries with the official source code, but it can be very bumpy.

I tried this method, but it didn't work. Since we only need to implement a small function to open pdf files, rather than implement a similar pdf reader, we changed a method, and we have to study it again when we do the reader.

Here is the process method to use the computer reader to open the file, the advantages are: simple, only two lines of code; The disadvantage is: the computer did not download the reader there is no way; The efficiency should not be as high as using the third square.

The code is as follows:


QString fileName = QFileDialog::getOpenFileName(this, tr(" Select the file "),QString(),
       tr("PDF  The document  (*.pdf);; All the files  (*.*)"));
 QProcess * p = new QProcess;
 p->start("C:\\Program Files (x86)\\Foxit Software\\Foxit Reader Plus\\FoxitReaderPlus.exe",
  QStringList() << fileName);

3, preview,

Preview USES the preview dialog box QPrintPreviewDialog, which is also a way to preview pdf by pictures. In fact, the preview window already has a print button, which can be printed in this interface. The code is as follows:


void MainWindow::on_btnPreview_clicked()
{
 QPrinter printer(QPrinter::HighResolution);
 // Customize paper size 
 printer.setPageSize(QPrinter::Custom);
 printer.setPaperSize(QSizeF(ui->stackedWidget->height(), ui->stackedWidget->width()),
    QPrinter::Point);
 QPrintPreviewDialog preview(&printer, this);
 preview.setMinimumSize(1000,600);
 connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreviewSlot(QPrinter*)));
 preview.exec ();
}

void MainWindow::printPreviewSlot(QPrinter *printerPixmap)
{
 printerPixmap->setOrientation(QPrinter::Landscape);
 // Get an image of the interface 
 QPixmap pixmap = QPixmap::grabWidget(ui->stackedWidget, ui->stackedWidget->rect());
 QPainter painterPixmap(this);
 painterPixmap.begin(printerPixmap);
 QRect rect = painterPixmap.viewport();
 int x = rect.width() / pixmap.width();
 int y = rect.height() / pixmap.height();
 painterPixmap.scale(x, y);
 painterPixmap.drawPixmap(0, 0, pixmap);
 painterPixmap.end();
}

4, print,

The printing dialog box QPrintDialog is used for printing. If you want to print the content of the text box, you can use the function print() directly. Otherwise, you can use the way of printing pictures.


void MainWindow::on_btnPrint_clicked()
{
 //  Creating a printer object 
 QPrinter printer;
 //  Create the print dialog 
 QString printerName = printer.printerName();
 if( printerName.size() == 0)
 return;
 QPrintDialog dlg(&printer, this);
 // If there is a selected area in the editor, print the selected area 
 if (ui->textEdit->textCursor().hasSelection())
 dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
 //  If the print button is pressed in the dialog box, the print operation is performed 
 if (dlg.exec() == QDialog::Accepted)
 {
 ui->textEdit->print(&printer);
 }
}

void MainWindow::on_btnPrint_2_clicked()
{
 QPrinter printerPixmap;
 QPixmap pixmap = QPixmap::grabWidget(ui->stackedWidget, ui->stackedWidget->rect()); // Get an image of the interface 
 QPrintDialog print(&printerPixmap, this);
 if (print.exec())
 {
 QPainter painterPixmap;
 painterPixmap.begin(&printerPixmap);
 QRect rect = painterPixmap.viewport();
 int x = rect.width() / pixmap.width();
 int y = rect.height() / pixmap.height();
 painterPixmap.scale(x, y); 
 painterPixmap.drawPixmap(0, 0, pixmap); 
 painterPixmap.end();
 }
}

Related articles: