转载自点我呀
Date: 2016-6-15
Author: kagula
Introduction:
一个简单的打印和打印预览示例代码。
Environment:
[1]Windows 7 64bits
[2]Qt Creator 3.6.1
[3]Qt 5.6
头文件
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
-
- namespace Ui {
- class MainWindow;
- }
-
- class QPrinter;
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
-
- private:
- Ui::MainWindow *ui;
- private slots:
- void OnTestPrint();
- void OnTestPrintPreview();
-
- void printDocument(QPrinter *printer);
- };
-
- #endif // MAINWINDOW_H
源文件
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- #include <QMessageBox>
- #include <QDebug>
-
- #include <QPrinter>
- #include <QPrintDialog>
- #include <QPrintPreviewDialog>
-
- #include <QPainter>
-
-
-
-
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
-
- connect(ui->pbTestPrint,SIGNAL(released()),this,SLOT(OnTestPrint()));
-
-
- connect(ui->pbTestPrintPreview,SIGNAL(released()),this,SLOT(OnTestPrintPreview()));
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::OnTestPrint()
- {
- QPrinter printer(QPrinter::HighResolution);
- QPrintDialog dialog(&printer, this);
- if (dialog.exec() != QDialog::Accepted)
- return;
-
-
-
-
-
-
-
-
- qDebug("The user has choiced printer.");
-
- printDocument(&printer);
- }
-
-
- void MainWindow::OnTestPrintPreview()
- {
- QPrinter printer(QPrinter::HighResolution);
- QPrintPreviewDialog preview(&printer, this);
- connect(&preview, SIGNAL(paintRequested(QPrinter*)),
- this, SLOT(printDocument(QPrinter*)));
- preview.exec();
- }
-
-
- void MainWindow::printDocument(QPrinter *printer)
- {
-
- QPainter painter;
- painter.begin(printer);
-
- QString family("Arial");
- QString style("Normal");
-
-
- QFont font(family, 32, 50, false);
- font.setStyleName(style);
- font = QFont(font, painter.device());
-
-
- QFontMetricsF fontMetrics(font);
- QRectF rect = fontMetrics.boundingRect(QString("%1 %2").arg(family).arg(style));
-
-
- qreal xScale = printer->pageRect().width() / rect.width();
- qreal yScale = printer->pageRect().height() / rect.height();
- double scale = qMin(xScale, yScale);
-
-
-
- painter.save();
-
- painter.translate(printer->pageRect().width() / 2.0, printer->pageRect().height() / 2.0);
-
- painter.scale(scale, scale);
-
-
- painter.setBrush(QBrush(Qt::white));
- painter.drawRect(0,0,rect.width()/2,rect.height());
- painter.setBrush(QBrush(Qt::black));
- painter.drawLine(0, 0, rect.width()/2, rect.height());
- painter.drawLine(0, rect.height(), rect.width()/2, 0);
-
-
- painter.drawText(QPointF(0,0),
- QString("%1-%2").arg(family).arg(style));
-
-
- painter.restore();
-
-
-
-
-
- painter.end();
- }
备注
[1]
经测试上文中测试待打印字符串宽度的
QFontMetricsF代码段是不正确的。
正确的应该参考下面的代码段,来得到在打印机上的
物理宽度,奇怪的是这种方式不能得到高度。
painter.
fontMetrics().
width(
instanceOfQString);
或 采用下面的初始化方式,能正确得到在打印机上的宽度和高度。
QFontMetricsF fm(instanceOfQFont,printer);//
QPrinter*printer
fm.
height()
补充阅读
[1]在win7下运行
QPrintPreviewDialog
的代码,不能选择打印机的问题
在pro文件中加入
QTPLUGIN += windowsprintersupport
部署exe程序的时候,把Qt安装目录 下的
printsupport子目录复制到exe所在的路径下即可。