c++ - QwebEnginePage不打印html代码Qt

标签 c++ qt qwebengineview

我尝试使用qwebenginepage打印HTML但无法正常工作,我进行了大量搜索以解决此问题,但未找到答案。

我曾经使用QTextBrowser,但是在打印时它会更改页面样式,因此我尝试使用qwebenginepage

QWebEnginePage *view = new QWebEnginePage;
QFile file("/home/hackerpoint/Desktop/some.html");
if(file.open(QIODevice::Text | QIODevice::ReadOnly)){
    QString html = file.readAll();
    QPrinter print(QPrinter::HighResolution);
    print.setPageSize(QPrinter::A4);
    print.setFullPage(true);
    print.setOutputFormat(QPrinter::PdfFormat);
    print.setOutputFileName("somefile.pdf");
    view->setHtml(html);
    if(&QWebEnginePage::loadFinished){
        view->print(&print , [=](bool){});
    }
    else {
        qInfo() << "Html Not loaded";
    }
}

这是错误
1  QPrinter::toPage() const                                                                      
2 QtWebEngineCore::PrinterWorker::print()                                                
3  QtWebEngineCore::PrinterWorker::qt_static_metacall(QObject *, QMetaObject::Call, int, void * *)
4  QObject::event(QEvent *)                                                                        
5  QApplicationPrivate::notify_helper(QObject *, QEvent *)                                         
6  QApplication::notify(QObject *, QEvent *)                                                       
7  QCoreApplication::notifyInternal2(QObject *, QEvent *)                                         
8  QCoreApplicationPrivate::sendPostedEvents(QObject *, int, QThreadData *)                        
9  postEventSourceDispatch(_GSource *, int ( *)(void *), void *)                                   
10 g_main_context_dispatch                                                                         
11 ___lldb_unnamed_symbol354$$libglib-2.0.so.0                                                     
12 g_main_context_iteration                                                                        
13 QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)                      
14 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)                                         
15 QThread::exec()                                                                                 
16 QThreadPrivate::start(void *)                                                                   
17 start_thread                                                                                    
18 __GI___clone                                                                                    

最佳答案

您会混淆这些概念:loadFinished是信号,执行if(&QWebEnginePage::loadFinished)没有任何意义(我建议您回顾一下信号和插槽的基本概念,因为它是Qt的基础:https://doc.qt.io/qt-5/signalsandslots.html)。

另一方面,没有必要将html加载为文件,因为可以将其传递给url。

考虑到上述内容,以下代码是如何打印的简单示例:

#include <QtWebEngineWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWebEnginePage page;
    QPrinter printer(QPrinter::HighResolution);
    printer.setPageSize(QPrinter::A4);
    printer.setFullPage(true);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName("somefile.pdf");
    QObject::connect(&page, &QWebEnginePage::loadFinished, [&page, &printer](bool ok){
        if(ok){
            page.print(&printer, [](bool ok){
                qDebug() << "printed " << ok;
                QCoreApplication::quit();
            });
        }
        else{
            qDebug() << "Html Not loaded";
            QCoreApplication::exit(-1);
        }
    });
    page.load(QUrl::fromLocalFile("/home/hackerpoint/Desktop/some.html"));
    return a.exec();
}

更新:

印表机类别

#ifndef PRINTER_H
#define PRINTER_H

#include <QObject>
#include <QPrinter>
#include <QWebEnginePage>

class Printer: public QObject
{
    Q_OBJECT
public:
    enum PrinterError{
        NoError = 0,
        LoadError,
        PrintError
    };
    Q_ENUM(PrinterError);
    Printer(QObject *parent=nullptr);
public Q_SLOTS:
    void print(const QString & html, const QUrl &baseUrl=QUrl());
    void print(const QUrl & url);
Q_SIGNALS:
    void finished();
    void error(PrinterError error);
private Q_SLOTS:
    void onFinished(bool ok);
private:
    QPrinter printer;
    QWebEnginePage page;
};

#endif // PRINTER_H
#include "printer.h"

Printer::Printer(QObject *parent):QObject(parent)
{
    printer.setResolution(QPrinter::HighResolution);
    printer.setPageSize(QPrinter::A4);
    printer.setFullPage(true);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName("somefile.pdf");
    connect(&page, &QWebEnginePage::loadFinished, this, &Printer::onFinished);
}

void Printer::print(const QString &html, const QUrl & baseUrl)
{
    page.setHtml(html, baseUrl);
}

void Printer::print(const QUrl &url)
{
    page.load(url);
}

void Printer::onFinished(bool ok)
{
    if(ok){
        page.print(&printer, [this](bool ok){
            if(ok)
                Q_EMIT finished();
            else
                Q_EMIT error(PrintError);
        });
    }
    else
        Q_EMIT error(LoadError);
}

MainWindow类:

// ...
private slots:
    void on_pushButton_clicked();
    void printError(Printer::PrinterError error);
    void printFinished();
private:
    Ui::MainWindow *ui;
    Printer printer;
// ...

// ...
    ui->setupUi(this);
    connect(&printer, &Printer::finished, this, &MainWindow::printFinished);
    connect(&printer, &Printer::error, this, &MainWindow::printError);
// ...
void MainWindow::on_pushButton_clicked()
{
    printer.print(QUrl::fromLocalFile("/home/hackerpoint/Desktop/some.html"));
}

void MainWindow::printError(Printer::PrinterError error)
{
  if(error == Printer::LoadError){
      qDebug() << "Html Not loaded";
  }
  else if(error == Printer::PrintError){
      qDebug() << "Html Not printed";
  }
}

void MainWindow::printFinished()
{
    qDebug() << "printed";
}

关于c++ - QwebEnginePage不打印html代码Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894144/

相关文章:

c++ - 匿名命名空间的 GCC 缺陷?

c# - 构建一个可以作为 Windows 服务运行的简单 Web 服务器

c++ - 语法错误 : unexpected type 'P_HDR'

c++ - Qt libao undefined reference

qt - 直接显示 9 声子错误 "Pins cannot connect"

c++ - 将 IPA 文件上传到 diawi.com

python - 如何自定义 python PyQt5 QWebEngineView 上下文菜单?

python - QWebEngineView - 如何在系统浏览器中打开链接

c++ - QT 离屏渲染保持黑色

c++ - 如何将 QWebEngineUrlRequestInfo 重定向到本地文件?