2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
QWebEnginePage
Is a class in the Qt WebEngine module that provides an interface for processing web page content. If you want to printQWebEnginePage
You can use the content inQPrinter
andQPrintDialog
class to implement it.
The project is configured with the Qt WebEngine module and includes the corresponding configuration in your .pro file:
QT += webenginewidgets
The implementation code is as follows, reference
printme « webenginewidgets « examples - qt/qtwebengine.git - Qt WebEngine
main.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
- #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
- #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";
-
-
- }
For https URL connections, the "Your connection is not a dedicated connection" alarm will appear. You need to skip this alarm, so you need to inherit QWebEnginePage to make a class
CustomWebEnginePage.h
- #pragma once
-
- #include <QWebEnginePage>
-
- class CustomWebEnginePage : public QWebEnginePage
- {
- Q_OBJECT
-
- public:
- CustomWebEnginePage();
- ~CustomWebEnginePage();
- virtual bool certificateError(const QWebEngineCertificateError &certificateError);
- };
CustomWebEnginePage.cpp
- #include "CustomWebEnginePage.h"
-
- CustomWebEnginePage::CustomWebEnginePage()
- {
- }
-
- CustomWebEnginePage::~CustomWebEnginePage()
- {
- }
-
-
- bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError)
- {
- return true;
- }