Qt呼叫印表機和印表機預覽程式碼
阿新 • • 發佈:2019-02-18
轉載自點我呀
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 :
- {
- 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>
- /*
- * Qt5列印支援 QT += printsupport
- * */
-
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;
- //預設為零,如果使用者選擇了列印範圍,以1為基數。
- //printer.fromPage();
- //printer.toPage();
- //設定列印範圍,以1為基數。
- //printer.setFromTo(1, LastNumberOfPage);
- 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)
- {
- //http://doc.qt.io/qt-5/qpainter.html
- 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));
- //如果不scale的話,會因為列印的字太小而看不見。
- qreal xScale = printer->pageRect().width() / rect.width();
- qreal yScale = printer->pageRect().height() / rect.height();
- double scale = qMin(xScale, yScale);
- //
- //Saves the current painter state (pushes the state onto a stack).
- painter.save();
- //Translates the coordinate system by the given offset;
- painter.translate(printer->pageRect().width() / 2.0, printer->pageRect().height() / 2.0);
- //Scales the coordinate system by (sX, sY).
- painter.scale(scale, scale);
- //Background x character for assure the bound and string draw orientation.
- 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);
- //Notice string vertical orientation in printer is negative from screen.
- painter.drawText(QPointF(0,0),
- QString("%1-%2").arg(family).arg(style));
- //Restores the current painter state (pops a saved state off the stack).
- painter.restore();
- //before begin new page.
- //printer->newPage();
- //after all done.
- 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所在的路徑下即可。