服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Qt实现保存、浏览、预览、打印功能的示例代码

Qt实现保存、浏览、预览、打印功能的示例代码

2021-06-17 14:07画茧自缚 C/C++

下面小编就为大家分享一篇Qt实现保存、浏览、预览、打印功能的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

Qt提供了以文本、图片、HTML等方式来实现对文档的操作,主要用到了QPrinter类和QPainter类,用到了QFileDialog文件窗口、QPrintPreviewDialog预览窗口类和QPrintDialog打印窗口类,Qt5也提供了QPdfWriter类来实现对pdf的操作,这里并不包括打开pdf文件,Qt没有提供任何方法来直接像文件浏览器一样打开pdf文件,可以用第三方库来实现。

这里采用了图片的方式来实现保存、预览和打印,其实 三个功能基本上一样。

1、保存PDF

(1)保存某个控件里的内容

一些输入类控件可以直接调用print()函数,一些显示类的控件可以直接调用render()函数,一些控件不具备这个功能。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void MainWindow::on_btnSave_clicked()
{
 QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), QString(), "*.pdf");
 if (!fileName.isEmpty())
 {
 // 如果文件后缀为空,则默认使用.pdf
 if (QFileInfo(fileName).suffix().isEmpty())
 {
  fileName.append(".pdf");
 }
 QPrinter printer;
 // 指定输出格式为pdf
 printer.setOutputFormat(QPrinter::PdfFormat);
 printer.setOutputFileName(fileName);
 // ui->textEdit->print(&printer);
 ui->tableWidget->render(&printer);
 }
}

(2)保存某些控件里的内容

这里需要将要保存的所有内容放在一个容器里面,比如放在QWidget上,同样可以用上面的方式来保存,下面用的是以图片的方式来保存。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void MainWindow::on_btnSave_clicked()
{
 QString fileName = QFileDialog::getSaveFileName(this, tr("保存PDF文件"), QString(), "*.pdf");
 if (!fileName.isEmpty())
 {
 // 如果文件后缀为空,则默认使用.pdf
 if (QFileInfo(fileName).suffix().isEmpty())
 {
  fileName.append(".pdf");
 }
 QPrinter printerPixmap(QPrinter::HighResolution);
 //自定义纸张大小,这里要打印的内容都在stackedWidget上
 printerPixmap.setPageSize(QPrinter::Custom);
 printerPixmap.setPaperSize(QSizeF(ui->stackedWidget->height(), ui->stackedWidget->width()), QPrinter::Point);
 //设置纸张大小为A4,这里注释掉了,建议自定义纸张 ,否则保存的就会有很多空白
 //printerPixmap.setPageSize(QPrinter::A4);
 //横向打印
 printerPixmap.setOrientation(QPrinter::Landscape);
 //设置输出格式为pdf
 printerPixmap.setOutputFormat(QPrinter::PdfFormat);
 //设置输出路径
 printerPixmap.setOutputFileName(fileName);
 //获取界面的图片
 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();
 //将图像(所有要画的东西)在pdf上按比例尺缩放
 painterPixmap.scale(x, y);
 //画图
 painterPixmap.drawPixmap(0, 0, pixmap);
 painterPixmap.end();
 QMessageBox::information(this, tr("生成PDF"), tr("保存PDF文件成功"), QMessageBox::Ok);
 }
}

2、浏览

Qt没有提供浏览pdf的方式,可以通过使用第三方库Poppler来实现,这里是相关文件:官网编译好的库文件所有文件的文件包实现pdf阅读器。可以通过官方的源码来编译库,不过可能会非常坎坷。

我试了这个方法,不过没有成功,(⊙﹏⊙)b!因为只是需要实现打开pdf文件的一个小功能而不是实现类似pdf阅读器,所以就换了一个方法,回头搞阅读器的时候还是得研究一番。

这里用的是进程的方法来使用电脑上的阅读器打开文件,优点是:简单,就两行代码;缺点是:①电脑上没下载阅读器就没办法了;②效率应该没有使用第三方库高。

代码如下:

?
1
2
3
4
5
QString fileName = QFileDialog::getOpenFileName(this, tr("选择文件"),QString(),
       tr("PDF 文档 (*.pdf);;所有文件 (*.*)"));
 QProcess * p = new QProcess;
 p->start("C:\\Program Files (x86)\\Foxit Software\\Foxit Reader Plus\\FoxitReaderPlus.exe",
  QStringList() << fileName);

3、预览

预览使用了预览对话框QPrintPreviewDialog,也是用的图片的方式来预览pdf,其实预览窗口已经自带了打印按钮,在这个界面已经可以打印了。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
void MainWindow::on_btnPreview_clicked()
{
 QPrinter printer(QPrinter::HighResolution);
 //自定义纸张大小
 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 ();
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void MainWindow::printPreviewSlot(QPrinter *printerPixmap)
{
 printerPixmap->setOrientation(QPrinter::Landscape);
 //获取界面的图片
 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、打印

打印使用了打印对话框QPrintDialog,如何打印文本框内容的话直接用print()函数就行,否则还是用打印图片的方式,和预览不同的其实就是使用的窗口类不同其余都差不多,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void MainWindow::on_btnPrint_clicked()
{
 // 创建打印机对象
 QPrinter printer;
 // 创建打印对话框
 QString printerName = printer.printerName();
 if( printerName.size() == 0)
 return;
 QPrintDialog dlg(&printer, this);
 //如果编辑器中有选中区域,则打印选中区域
 if (ui->textEdit->textCursor().hasSelection())
 dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
 // 如果在对话框中按下了打印按钮,则执行打印操作
 if (dlg.exec() == QDialog::Accepted)
 {
 ui->textEdit->print(&printer);
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void MainWindow::on_btnPrint_2_clicked()
{
 QPrinter printerPixmap;
 QPixmap pixmap = QPixmap::grabWidget(ui->stackedWidget, ui->stackedWidget->rect()); //获取界面的图片
 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();
 }
}

以上这篇Qt实现保存、浏览、预览、打印功能的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/kasama1953/article/details/51749372

延伸 · 阅读

精彩推荐