Qt 提供廣泛跨平颱打印支持。使用各平颱打印係統,Qt 應用程序可以打印到附加打印機,和跨網絡到遠程打印機。Qt 打印係統還支持 PDF 文件生成,為生成基本報告提供基礎設施。
以下類支持打印機的選擇、設置及打印輸齣。
| 為用於配置打印機的打印對話框的基實現 | |
| 用於打印機頁麵相關選項的配置對話框 | |
| 用於指定打印機配置的對話框 | |
| 定義 QPrinter 如何與給定打印子係統交互的接口 | |
| 為打印機輸齣而預覽和配置頁麵布局的 Widget | |
| 為打印機輸齣而預覽頁麵布局的 Widget | |
| 在打印機上描繪的描繪設備 | |
| 給予訪問現有打印機的有關信息 |
在 Qt,打印機的錶示通過 QPrinter ,描繪設備提供特定打印功能 (譬如:支持多頁和雙麵輸齣)。因此,打印涉及使用 QPainter 以如在自定義 Widget 或圖像上描繪的相同方式,在一係列頁麵上描繪。
盡管 QPrinter 對象不要求用戶輸入就可以被構建和設置,打印經常是根據用戶的請求而履行的;例如,當用戶選擇 文件|打印... 菜單項在 GUI 應用程序中。在這種情況下,新近構造的 QPrinter 對象被供給給 QPrintDialog ,允許用戶指定要使用的打印機、紙張尺寸及其它打印特性。
QPrinter printer;
QPrintDialog dialog(&printer, this);
dialog.setWindowTitle(tr("Print Document"));
if (editor->textCursor().hasSelection())
dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
if (dialog.exec() != QDialog::Accepted) {
return;
}
設置某些默認特性也是可能的,通過修改 QPrinter 在被提供給打印對話框之前。例如,生成批量打印報告的應用程序,可能設置 QPrinter to 寫入本地文件 默認情況下而不是打印機。
一旦 QPrinter 對象被構造並設置, QPainter 可以用來在其上履行描繪操作。可以按以下方式構造和設置描繪器:
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
for (int page = 0; page < numberOfPages; ++page) {
// Use the painter to draw on the page.
if (page != lastPage)
printer.newPage();
}
painter.end();
由於 QPrinter 以空白頁開頭,我們隻需調用 newPage() 函數在繪製每頁之後,除最後一頁外。
文檔被發送給打印機 (或寫入本地文件),當我們調用 end() .
QPrinter provides functions that can be used to obtain information about the dimensions of the paper (the paper rectangle) and the dimensions of the printable area (the page rectangle). These are given in logical device coordinates that may differ from the physical coordinates used by the device itself, indicating that the printer is able to render text and graphics at a (typically higher) resolution than the user's display.
Although we do not need to handle the conversion between logical and physical coordinates ourselves, we still need to apply transformations to painting operations because the pixel measurements used to draw on screen are often too small for the higher resolutions of typical printers.
|
打印機和描繪器坐標係
The paperRect() and pageRect() functions provide information about the size of the paper used for printing and the area on it that can be painted on. The rectangle returned by pageRect() usually lies inside the rectangle returned by paperRect() . You do not need to take the positions and sizes of these area into account when using a QPainter 采用 QPrinter as the underlying paint device; the origin of the painter's coordinate system will coincide with the top-left corner of the page rectangle, and painting operations will be clipped to the bounds of the drawable part of the page. |
|
The paint system automatically uses the correct device metrics when painting text but, if you need to position text using information obtained from font metrics, you need to ensure that the print device is specified when you construct QFontMetrics and QFontMetricsF objects, or ensure that each QFont used is constructed using the form of the constructor that accepts a QPaintDevice 自變量。
要打印 Widget,可以使用 QWidget::render () 函數。如前所述,打印機分辨率通常高於屏幕分辨率,因此您必須縮放描繪器。您可能還想要在頁麵上放置 Widget。以下代碼範例展示這可能看起來如何。
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width() / double(myWidget->width());
double yscale = printer.pageRect().height() / double(myWidget->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
myWidget->render(&painter);
某些 Widget,譬如 QTextEdit and QGraphicsView , display rich content that is typically managed by instances of other classes, such as QTextDocument and QGraphicsScene . As a result, it is these content handling classes that usually provide printing functionality, either via a function that can be used to perform the complete task, or via a function that accepts an existing QPainter object. Some widgets provide convenience functions to expose underlying printing features, avoiding the need to obtain the content handler just to call a single function.
The following table shows which class and function are responsible for printing from a selection of different widgets. For widgets that do not expose printing functionality directly, the content handling classes containing this functionality can be obtained via a function in the corresponding widget's API.
| Widget | 打印函數 | 接受 |
|---|---|---|
| QGraphicsView | QGraphicsView::render () | QPainter |
| QSvgWidget | QSvgRenderer::render() | QPainter |
| QTextEdit | QTextDocument::print () | QPrinter |
| QTextLayout | QTextLayout::draw () | QPainter |
| QTextLine | QTextLine::draw () | QPainter |
QTextEdit 要求 QPrinter 而不是 QPainter because it uses information about the configured page dimensions in order to insert page breaks at the most appropriate places in printed documents.
Qt Print Support 模塊在商業許可下是可用的來自 Qt 公司 。此外,它在自由軟件許可下也是可用的。從 Qt 5.4 起,這些自由軟件許可是 GNU LGPL (次一般公共許可) 第 3 版 ,或 GNU GPL (一般公共許可) 第 2 版 。見 Qt 許可 進一步瞭解細節。
Please note that Adobe ® places restrictions on the use of its trademarks (including logos) in conjunction with PDF; e.g. "Adobe PDF". Please refer to www.adobe.com for guidelines.