Qt调用打印机和打印机预览代码

转载自点我呀

Date:  2016-6-15

Author: kagula

Introduction:

       一个简单的打印和打印预览示例代码。

Environment:

[1]Windows 7 64bits

[2]Qt Creator 3.6.1

[3]Qt 5.6


头文件

[cpp]  view plain  copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5.   
  6. namespace Ui {  
  7. class MainWindow;  
  8. }  
  9.   
  10. class QPrinter;  
  11.   
  12. class MainWindow : public QMainWindow  
  13. {  
  14.     Q_OBJECT  
  15.   
  16. public:  
  17.     explicit MainWindow(QWidget *parent = 0);  
  18.     ~MainWindow();  
  19.   
  20. private:  
  21.     Ui::MainWindow *ui;  
  22. private slots:  
  23.     void OnTestPrint();  
  24.     void OnTestPrintPreview();  
  25.   
  26.     void printDocument(QPrinter *printer);  
  27. };  
  28.   
  29. #endif // MAINWINDOW_H  


源文件

[cpp]  view plain  copy
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3.   
  4. #include <QMessageBox>  
  5. #include <QDebug>  
  6.   
  7. #include <QPrinter>  
  8. #include <QPrintDialog>  
  9. #include <QPrintPreviewDialog>  
  10.   
  11. #include <QPainter>  
  12.   
  13. /* 
  14.  * Qt5打印支持  QT += printsupport 
  15.  * */  
  16.   
  17. MainWindow::MainWindow(QWidget *parent) :  
  18.     QMainWindow(parent),  
  19.     ui(new Ui::MainWindow)  
  20. {  
  21.     ui->setupUi(this);  
  22.   
  23.     //打印  
  24.     connect(ui->pbTestPrint,SIGNAL(released()),this,SLOT(OnTestPrint()));  
  25.   
  26.     //打印预览  
  27.     connect(ui->pbTestPrintPreview,SIGNAL(released()),this,SLOT(OnTestPrintPreview()));  
  28. }  
  29.   
  30. MainWindow::~MainWindow()  
  31. {  
  32.     delete ui;  
  33. }  
  34.   
  35. void MainWindow::OnTestPrint()  
  36. {  
  37.     QPrinter printer(QPrinter::HighResolution);  
  38.     QPrintDialog dialog(&printer, this);  
  39.     if (dialog.exec() != QDialog::Accepted)  
  40.         return;  
  41.   
  42.     //默认为零,如果用户选择了打印范围,以1为基数。  
  43.     //printer.fromPage();  
  44.     //printer.toPage();  
  45.   
  46.     //设置打印范围,以1为基数。  
  47.      //printer.setFromTo(1, LastNumberOfPage);  
  48.   
  49.     qDebug("The user has choiced printer.");  
  50.   
  51.     printDocument(&printer);  
  52. }  
  53.   
  54. //测试打印预览功能  
  55. void MainWindow::OnTestPrintPreview()  
  56. {  
  57.     QPrinter printer(QPrinter::HighResolution);  
  58.     QPrintPreviewDialog preview(&printer, this);  
  59.     connect(&preview, SIGNAL(paintRequested(QPrinter*)),  
  60.             this, SLOT(printDocument(QPrinter*)));  
  61.     preview.exec();  
  62. }  
  63.   
  64.   
  65. void MainWindow::printDocument(QPrinter *printer)  
  66. {  
  67.     //http://doc.qt.io/qt-5/qpainter.html  
  68.     QPainter painter;  
  69.     painter.begin(printer);  
  70.   
  71.     QString family("Arial");  
  72.     QString style("Normal");  
  73.   
  74.     //  
  75.     QFont font(family, 32, 50, false);  
  76.     font.setStyleName(style);  
  77.     font = QFont(font, painter.device());  
  78.   
  79.     //  
  80.     QFontMetricsF fontMetrics(font);  
  81.     QRectF rect = fontMetrics.boundingRect(QString("%1 %2").arg(family).arg(style));  
  82.   
  83.     //如果不scale的话,会因为打印的字太小而看不见。  
  84.     qreal xScale = printer->pageRect().width() / rect.width();  
  85.     qreal yScale = printer->pageRect().height() / rect.height();  
  86.     double scale = qMin(xScale, yScale);  
  87.   
  88.     //  
  89.     //Saves the current painter state (pushes the state onto a stack).  
  90.     painter.save();  
  91.     //Translates the coordinate system by the given offset;  
  92.     painter.translate(printer->pageRect().width() / 2.0, printer->pageRect().height() / 2.0);  
  93.     //Scales the coordinate system by (sX, sY).  
  94.     painter.scale(scale, scale);  
  95.   
  96.     //Background x character for assure the bound and string draw orientation.  
  97.     painter.setBrush(QBrush(Qt::white));  
  98.     painter.drawRect(0,0,rect.width()/2,rect.height());  
  99.     painter.setBrush(QBrush(Qt::black));  
  100.     painter.drawLine(0, 0, rect.width()/2, rect.height());  
  101.     painter.drawLine(0, rect.height(), rect.width()/2, 0);  
  102.   
  103.     //Notice string vertical orientation in printer is negative from screen.  
  104.     painter.drawText(QPointF(0,0),  
  105.                      QString("%1-%2").arg(family).arg(style));  
  106.   
  107.     //Restores the current painter state (pops a saved state off the stack).  
  108.     painter.restore();  
  109.   
  110.     //before begin new page.  
  111.     //printer->newPage();  
  112.   
  113.     //after all done.  
  114.     painter.end();  
  115. }  

备注
[1]
经测试上文中测试待打印字符串宽度的 QFontMetricsF代码段是不正确的。
正确的应该参考下面的代码段,来得到在打印机上的 物理宽度,奇怪的是这种方式不能得到高度。
painter. fontMetrics(). width( instanceOfQString);
或 采用下面的初始化方式,能正确得到在打印机上的宽度和高度。
QFontMetricsF fm(instanceOfQFont,printer);// QPrinter*printer
fm. height()
 

补充阅读
[1]在win7下运行 QPrintPreviewDialog 的代码,不能选择打印机的问题
在pro文件中加入
QTPLUGIN += windowsprintersupport
部署exe程序的时候,把Qt安装目录 下的 printsupport子目录复制到exe所在的路径下即可。

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值