QWebEnginePage
是 Qt WebEngine 模块中的一个类,它提供了用于处理网页内容的接口。如果你想要打印 QWebEnginePage
中的内容,你可以使用 QPrinter
和 QPrintDialog
类来实现。
项目配置了 Qt WebEngine 模块,并且在你的.pro文件中包含了相应的配置:
QT += webenginewidgets
实现代码如下,参考
printme << webenginewidgets << examples - qt/qtwebengine.git - Qt WebEngine
main.cpp
cpp
#include "printhandler.h"
#include <QApplication>
#include <QShortcut>
#include <QWebEngineView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
view.setUrl(QUrl(QStringLiteral("qrc:/index.html")));
view.resize(1024, 750);
view.show();
PrintHandler handler;
handler.setPage(view.page());
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);
return app.exec();
}
printhandler.h
cpp
#ifndef PRINTHANDLER_H
#define PRINTHANDLER_H
#include <qapplication.h>
#include <QObject>
#include <QWebEngineView>
#include <QPrinter>
#include <QPrintPreviewDialog>
#include <QEventLoop>
#include <QPrintDialog>
#include <QWidget>
#include <QMainWindow>
#include <QPrinterInfo>
#include "CustomWebEnginePage.h"
#include <qapplication.h>
#include <qtimer.h>
class PrintHandler : public QObject
{
Q_OBJECT
public:
explicit PrintHandler(QString printName, QObject *parent = nullptr);
signals:
public slots:
public:
void setPrintUrl(const QString& url);
void setPrintName(const QString& name);
void printPreview();
void printDocument(QPrinter *printer);
void print();
private:
//void print();
private:
QString mPrinterName;
CustomWebEnginePage* mPage;
QString mUrl;
bool mInPrintPreview;
QTimer* waitTimer;
};
#endif // PRINTHANDLER_H
printhandler.cpp
cpp
#include "printhandler.h"
#include <QDebug>
PrintHandler::PrintHandler(QString printName,QObject *parent) : mPrinterName(printName),QObject(parent)
{
mInPrintPreview=false;
mPage = new CustomWebEnginePage();
waitTimer = new QTimer();
waitTimer->setInterval(500);
waitTimer->setSingleShot(true);
connect(waitTimer, &QTimer::timeout, this, &PrintHandler::print);
connect(mPage, &CustomWebEnginePage::loadFinished, waitTimer, static_cast<void(QTimer::*)()>(&QTimer::start));
}
void PrintHandler::setPrintUrl(const QString& url)
{
mUrl = url;
mPage->setUrl(url);
}
void PrintHandler::printPreview()
{
if (!mPage)
return;
if (mInPrintPreview)
return;
mInPrintPreview = true;
QPrinter printer;
QPrintPreviewDialog preview(&printer, mPage->view());
connect(&preview, &QPrintPreviewDialog::paintRequested,
this, &PrintHandler::printDocument);
preview.exec();
mInPrintPreview = false;
}
void PrintHandler::printDocument(QPrinter *printer)
{
QEventLoop loop;
bool result;
auto printPreview = [&](bool success) { result = success; loop.quit(); };
mPage->print(printer, std::move(printPreview));
loop.exec();
if (!result) {
QPainter painter;
if (painter.begin(printer)) {
QFont font = painter.font();
font.setPixelSize(15);
painter.setFont(font);
painter.drawText(QPointF(10,25),QStringLiteral("Could not generate print preview."));
painter.end();
}
}
}
void PrintHandler::setPrintName(const QString& name)
{
mPrinterName = name;
}
void PrintHandler::print()
{
QStringList printerNameList=QPrinterInfo::availablePrinterNames();
foreach (QString printerStr, printerNameList)
{
if(printerStr== mPrinterName)
{
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::NativeFormat);
printer.setPrinterName(printerStr);
//打印界面
/* QPrintDialog dialog(&printer, mPage->view());
if (dialog.exec() != QDialog::Accepted)
return;*/
printDocument(&printer);
return;
}
}
qDebug() << "not find printer";
}
对于https的url连接,会出现"你的连接不是专用连接"报警,需要跳过这个报警所以要继承QWebEnginePage做个类
CustomWebEnginePage.h
cpp
#pragma once
#include <QWebEnginePage>
class CustomWebEnginePage : public QWebEnginePage
{
Q_OBJECT
public:
CustomWebEnginePage();
~CustomWebEnginePage();
virtual bool certificateError(const QWebEngineCertificateError &certificateError);
};
CustomWebEnginePage.cpp
cpp
#include "CustomWebEnginePage.h"
CustomWebEnginePage::CustomWebEnginePage()
{
}
CustomWebEnginePage::~CustomWebEnginePage()
{
}
bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError)
{
return true;
}