您现在的位置是:网站首页> C/C++
Qt调用打印机和打印机预览代码
- C/C++
- 2021-11-11
- 982人已阅读
摘要
一个简单的打印和打印预览示例代码
可能是由于内容开始是网络地址(http://...)完整的英文词截取后不能换行造成 为避免代码造成手机端排版的混乱,可适当增加文字描述,将代码往后推移
头文件
- #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>
- /*
- * 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();
- }
下一篇:Qt 打印功能