当前位置:网站首页>[QT] QT 5 procedure: print documents
[QT] QT 5 procedure: print documents
2022-06-25 08:15:00 【Cappuccino-jay】
Qt 5 Medium Qt Print Support The module provides support for printing .
The simplest , Just use one QPrinter Class and a print dialog QPrintDialog Class can complete the printing operation of the document .
Next, the program can print documents 、 Print preview and production PDF Document and other operations .
More applications can be found in the help by Qt Print Support Keyword view .
1、 Create a new one Qt Widgets application , The project name can be customized ( for example :printFile)
Class names and base classes keep MainWindow and QMainWindow unchanged . After completion , open printFile.pro file , Add the following line of code :
QT += printsupport
2、 Then go to the design mode and drag a... Into the interface Text Edit . Until then mainwindow.h Add the pre declaration of the class in the file first :
class QPrinter;
3、 Then add several slot function declarations :
private slots:
void doPrint();
void doPrintPreview();
void printPreview(QPrinter * printer);
void createPdf();
4、 The following to mainwindow.cpp Add header file to file :
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QFileDialog>
#include <QFileInfo>
5、 Define several actions in the constructor :
QAction * action_print = new QAction(tr(" Print "),this);
QAction * action_printPreview = new QAction(tr(" Print preview "),this);
QAction * action_pdf = new QAction(tr(" Generate pdf"),this);
connect(action_print,SIGNAL(triggered()),this,SLOT(doPrint()));
connect(action_printPreview,SIGNAL(triggered()),this,SLOT(doPrintPreview()));
connect(action_pdf,SIGNAL(triggered()),this,SLOT(createPdf()));
ui->mainToolBar->addAction(action_print);
ui->mainToolBar->addAction(action_printPreview);
ui->mainToolBar->addAction(action_pdf);
6、 Then add those Slot function The definition of :
void MainWindow::doPrint() // Print
{
QPrinter printer; // Create printer object
QPrintDialog dlg(&printer,this); // Create printer dialog
// If there is a selected area in the editor , Print the selected area
if(ui->textEdit->textCursor().hasSelection())
dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
if(dlg.exec() == QDialog::Accepted){
// If the print button is pressed in the dialog box
ui->textEdit->print(&printer); // Then execute the printing operation
}
}
Here we first establish QPrinter Class object , It represents a printing device . Then a print dialog is created , If there is a selected area in the editor , Then print the area , Otherwise, print the entire page .
void MainWindow::doPrintPreview() // Print preview
{
QPrinter printer;
QPrintPreviewDialog preview(&printer,this); // Create print preview dialog box
// When you want to generate a preview page , launch paintRequested() The signal
connect(&preview,&QPrintPreviewDialog::paintRequested,this,&MainWindow::printPreview);
preview.exec();
}
void MainWindow::printPreview(QPrinter * printer)
{
ui->textEdit->print(printer);
}
Here, the print preview dialog box is mainly used for print preview , To associate it paintRequested() Signal to the customized slot , The print function of the editor must be called in the slot , And with the coming QPrinter Class takes an object pointer as a parameter .
void MainWindow::createPdf() // Generate PDF file
{
QString fileName = QFileDialog::getSaveFileName(this,tr(" export PDF file "),QString(),"*.pdf");
if(!fileName.isEmpty()){
if(QFileInfo(fileName).suffix().isEmpty())
fileName.append(".pdf"); // If the file suffix is empty , It is used by default .pdf
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat); // Specify the output format as pdf
printer.setOutputFileName(fileName);
ui->textEdit->print(&printer);
}
}
It's generating PDF In the slot of the document , Use the file dialog box to get the path to save the file ; If the file name does not specify a suffix , Then add “.pdf” suffix . And then to QPrinter Object to specify the output format and file path , This will print the document as PDF Format .
7、 Finally, you can run the program , If the computer has a printer installed , Then you can test the actual printing effect , Otherwise, it can only be tested PDF Printing effect .

Print preview dialog effect :

边栏推荐
- Drawing of clock dial
- Deep learning series 48:deepfaker
- 想转行学软件测试担心哪些问题?
- DNS protocol and its complete DNS query process
- 力扣 272. 最接近的二叉搜索树值 II 递归
- Bat start NET Core
- 函数尽量不要通过变量指定操作类型
- Wechat applet introduction record
- Luogu p5994 [pa2014]kuglarz (XOR thinking +mst)
- Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
猜你喜欢

Electronics: Lesson 010 - Experiment 8: relay oscillator

剑指offer刷题(中等等级)

剑指offer刷题(简单等级)

Apache CouchDB Code Execution Vulnerability (cve-2022-24706) batch POC

Electronics: Lesson 008 - Experiment 6: very simple switches

PH neutralization process modeling

Neural network and deep learning-3-simple example of machine learning pytorch

静态网页服务器

Stm32cubemx learning (5) input capture experiment

TCP stuff
随机推荐
Matlab code format one click beautification artifact
Est - il sûr d'ouvrir un compte d'actions maintenant via le lien d'ouverture de compte coiffé?
静态网页服务器
Deep learning series 48:deepfaker
Wechat applet opening customer service message function development
力扣 272. 最接近的二叉搜索树值 II 递归
What is SKU and SPU? What is the difference between SKU and SPU
唐老师讲运算放大器(第七讲)——运放的应用
是否可以给数据库表授予删除列对象的权限?为什么?
电子学:第011课——实验 10:晶体管开关
LeetCode_ Hash table_ Medium_ 454. adding four numbers II
Set the textalign property of the label control in C to control the method of text centering
Modeling and fault simulation of aircraft bleed system
Opencv minimum filtering (not limited to images)
[thesis study] vqmivc
剑指offer刷题(简单等级)
Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer
Solving some interesting problems with recurrence of function
[supplementary question] 2021 Niuke summer multi school training camp 4-N
什么是SKU和SPU,SKU,SPU的区别是什么