本文來研究一下Qt Graphics-View的打印功能實現。
在Qt的官方文檔中介紹了Graphics-View的打印相關內容。
函數
Qt中對打印的支持是有一個獨立的printsupport模塊來完成的,因此,要想在程序中使用Qt的打印功能,必須先在pro文件中添加下面這句代碼:
QT += printsupportui
圖形視圖經過它的展現函數:QGraphicsScene::render()和QGraphicsView::render()提供單線(single-line)打印。這些函數提供相同的API,經過將QPainter傳遞給展現函數,你能夠打印場景、視圖的所有或部份內容。例子顯示瞭如何使用QPainter將場景的所有內容打印到整頁紙上。this
QGraphicsScene scene;
scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));spa
QPrinter printer;
if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
QPainter painter(&printer);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
}.net
場景和視圖函數展現函數的差別是一個在場景座標,另外一個在視圖座標。QGraphicsScene::render()經常使用於打印無變換的場景的所有內容,如畫幾何數據文檔等。QGraphicsView::render()適合於打印屏幕快照(screenshots),缺省狀況下,它展現視圖端口中的當前內容。debug
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");orm
當源區域和目標區域的大小不匹配時,源內容進行伸展以適合目標區域。經過傳遞Qt::AspectRatioMode給你正調用的展現函數,你能夠在源內容伸縮時,保持或忽略縱橫比。blog
下面是我實驗的代碼,僅供參考。ci
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(); } |