本文来研究一下Qt Graphics-View的打印功能实现。
在Qt的官方文档中介绍了Graphics-View的打印相关内容。
Qt中对打印的支持是有一个独立的printsupport模块来完成的,所以,要想在程序中使用Qt的打印功能,必须先在pro文件中添加下面这句代码:
QT += printsupport
图形视图通过它的展示函数:QGraphicsScene::render()和QGraphicsView::render()提供单线(single-line)打印。这些函数提供相同的API,通过将QPainter传递给展示函数,你可以打印场景、视图的全部或部分内容。例子显示了如何使用QPainter将场景的全部内容打印到整页纸上。
QGraphicsScene scene;
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
QPrinter printer;
if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
QPainter painter(&printer);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
}
场景和视图函数展示函数的差异是一个在场景坐标,另一个在视图坐标。QGraphicsScene::render()常用于打印无变换的场景的全部内容,如画几何数据文档等。QGraphicsView::render()适合于打印屏幕快照(screenshots),缺省情况下,它展示视图端口中的当前内容。
QGraphicsScene scene();
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
//QPixmap pixmap; 官当文档这样是错误的,pixmap初始化为空,没空间可不行
QPixmap pixmap(scene->sceneRect().width(), scene->sceneRect().height());
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
painter.end();
pixmap.save("scene.png");
当源区域和目标区域的大小不匹配时,源内容进行伸展以适合目标区域。通过传递Qt::AspectRatioMode给你正调用的展示函数,你可以在源内容伸缩时,保持或忽略纵横比。
下面是我实验的代码,仅供参考。
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
void MainWindow::printReview()
{ QPrinter printer(QPrinter::HighResolution); // 创建打印预览对话框 QPrintPreviewDialog preview(&printer, this, windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); // 当要生成预览页面时,发射paintRequested()信号 connect(&preview, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printPreview(QPrinter *))); preview.exec(); } void MainWindow::printPreview(QPrinter *printer) { QPainter painter(printer); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); m_pScene->render(&painter); } void MainWindow::printToPrinter() { QPrinter printer(QPrinter::HighResolution); if (QPrintDialog(&printer).exec() == QDialog::Accepted) { QPainter painter(&printer); painter.setRenderHint(QPainter::Antialiasing); m_pScene->render(&painter); } } void MainWindow::printToImage() { // 记住,一定要初始化QPixmap的大小 QPixmap pixmap(m_pScene->sceneRect().width(), m_pScene->sceneRect().height()); pixmap.fill(Qt::white); QPainter painter(&pixmap); painter.setRenderHint(QPainter::HighQualityAntialiasing); m_pView->render(&painter); painter.end(); QString filePath = QFileDialog::getSaveFileName(this, "Save Image", "", "BMP (*.bmp);;PNG (*.png);;JPEG (*.jpg *.jpeg);;All files (*.*)"); if (filePath == "" && pixmap.isNull()) { qDebug() << "Error!"; return; } pixmap.save(filePath); } void MainWindow::printToPdf() { QPrinter printer(QPrinter::HighResolution); printer.setPageSize(QPrinter::A4); printer.setOrientation(QPrinter::Portrait); printer.setOutputFormat(QPrinter::PdfFormat); // file will be created in your build directory (where debug/release directories are) printer.setOutputFileName("MyGV-OpenGL.pdf"); QPainter painter; if(!painter.begin(&printer)) { qDebug() << "Error!"; return; } m_pScene->render(&painter); painter.end(); } |