WebEngine ウィジェット PrintMe の例
QtWebEngine ウィジェットを使用して Web ページを印刷する方法を説明します。
PrintMe は、QWebEnginePage とQPrintDialog クラスを使用して Web ページを印刷する方法を示します。さらに、QPrintPreviewDialog クラスを使用して印刷プレビューを実装する方法を示します。完全を期すために、JavaScript内で印刷要求をトリガーする方法も示しています。
例の実行
から例を実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。
簡単なHTMLページ
この例では、リソース・コレクション・ファイル(.qrc)として追加される内部 HTML ページを作成します。このページには小さなHTMLメッセージボックスだけが表示され、キーボードショートカットの使用やボタンのクリックによって印刷をトリガーする方法を説明しています。ボタンにはJavaScriptのonclick
イベント属性があり、JavaScriptのwindow.print()
関数を呼び出します。
<html lang="en"> <head> <meta charset="utf-8"> <title>PrintMe</title> <link rel="stylesheet" type="text/css" href="style.css"> <script> function printNow() { window.print(); } </script> </head> <body> <form class="form"> <div class="header"> <h1>Hello Paper World!</h1> <h2>Press Ctrl+p to print with print preview</h2> <h2>Press Ctrl+Shift+p to print without print preview</h2> <h2>Click the button to print using JavaScript</h2> <p class="button" onclick="printNow()">Print Now</p> </div> </form> </body> </html>
メイン関数
main
関数では、まずQWebEngineView をインスタンス化し、内部 HTML ページへの URL を設定します。次に、PrintHandler
インスタンスを作成し、要求されたページを渡します。便宜上、印刷ダイアログや印刷プレビュー・ダイアログを呼び出すのに使用できるキーボード・ショートカットも作成します。
QWebEngineView view; view.setUrl(QUrl(QStringLiteral("qrc:/index.html"))); view.resize(1024, 750); view.show(); PrintHandler handler; handler.setView(&view); auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_P), &view); auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_P), &view); QObject::connect(printPreviewShortCut, &QShortcut::activated, &handler, &PrintHandler::printPreview); QObject::connect(printShortCut, &QShortcut::activated, &handler, &PrintHandler::print);
印刷ハンドラ
PrintHandler
クラスでは、まずprintPreview()
を実装し、QPrintPreviewDialog をインスタンス化します。プレビュー・ページのセットを生成するには、QPrintPreviewDialog::paintRequested ハンドルが必要です。
void PrintHandler::printPreview() { if (!m_view) return; if (m_inPrintPreview) return; m_inPrintPreview = true; QPrintPreviewDialog preview(&m_printer, m_view); connect(&preview, &QPrintPreviewDialog::paintRequested, this, &PrintHandler::printDocument); preview.exec(); m_inPrintPreview = false; }
QPrintPreviewDialog::paintRequested シグナルに応答して呼び出されるPrintHandler::printDocument()
スロットを実装します。プリンターに実際にペイントするには、QWebEngineView::print ()関数を呼び出します。印刷はChromiumでは非同期操作ですが、Qtでは非同期操作ではないので、QEventLoop::exec ()を使ってローカルイベントループを実行し、印刷が完了したことを確認してから戻ります。印刷が終わるのを待っている間にボタンをクリックすると、内部状態が混乱してクラッシュする可能性があるため、ユーザー入力はブロックされます。
void PrintHandler::printDocument(QPrinter *printer) { m_view->print(printer); // User input in the print preview dialog while we're waiting on a print task // can mess up the internal state and cause a crash. m_waitForResult.exec(QEventLoop::ExcludeUserInputEvents); }
印刷ジョブの結果を通知してもらうために、QWebEngineView::printFinished() シグナルのハンドラとしてPrintHandler::printFinished()
スロットを実装します。success
をチェックし、発生したエラーを報告する。最後に、QEventLoop::quit ()を呼び出して、ローカル・イベント・ループから抜けます。
void PrintHandler::printFinished(bool success) { if (!success) { QPainter painter; if (painter.begin(&m_printer)) { QFont font = painter.font(); font.setPixelSize(20); painter.setFont(font); painter.drawText(QPointF(10,25), QStringLiteral("Could not generate print preview.")); painter.end(); } } m_waitForResult.quit(); }
最後に実装する関数PrintHandler::print()
は、単にQPrintDialog を開き、先に実装したPrintHandler::printDocument()
を呼び出すだけなので、些細なものです。
void PrintHandler::print() { QPrintDialog dialog(&m_printer, m_view); if (dialog.exec() != QDialog::Accepted) return; printDocument(&m_printer); }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.