QWebPage / QPrinter - 无法加载页面

QWebPage / QPrinter - can't load page

本文关键字:加载 QPrinter QWebPage      更新时间:2023-10-16

我是新的Qt,我需要做一个程序谁导出html页面PDF

因此,主要思想是使用QWebPage来解释html,并使用QPrinter将其导出为pdf。

我有两个班webview使用QWebPagePrint使用QPrinter

main.cpp中,我将LoadFinished连接到PrintPDF插槽:

Print *pdf = new Print(args);
webview *nav = new webview();
nav->setPrinter(pdf->getPrinter());
if(nav->load(args) == false) {
    qDebug() << "can't load page";
    return 0;
}
//when the page page is ready, we will print it
QObject::connect(nav->getFrame(), SIGNAL(loadFinished(bool)), nav, SLOT(printPDF(bool)));

My webview.cpp class:

#include "webview.h"
webview::webview()
{
    page = new QWebPage;
    printer = 0;
}
webview::~webview()
{
    delete page;
}
bool webview::load(Arguments *args)
{
    QRegularExpression url("^(file|http)://");
    QRegularExpression fullPath("^/");
    QRegularExpressionMatch pathMatch = fullPath.match(args->getSource());
    QRegularExpressionMatch urlMatch = url.match(args->getSource());
    // see http://qt-project.org/doc/qt-4.8/qwebsettings.html#WebAttribute-enum for more informations
    page->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
    page->settings()->setAttribute(QWebSettings::AutoLoadImages, true);
    page->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    page->settings()->setAttribute(QWebSettings::PrintElementBackgrounds, true);
    page->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    if(pathMatch.hasMatch()) {
        page->mainFrame()->load(QUrl::fromLocalFile(args->getSource()));
    } else {
        if (urlMatch.hasMatch()) {
            page->mainFrame()->load(QUrl(args->getSource()));
        } else {
            fprintf(stderr, "%sn", qPrintable(QApplication::translate("main", "Error: Invalide source file")));
            return false;
        }
    }
    return true;
}
void webview::printPDF(bool ok)
{
    if(ok == true) {
        qDebug() << "okay";
    } else
        qDebug() << "non okay";
    if(printer != 0)
        page->mainFrame()->print(printer);
}

这是我的控制台显示:

non
QPainter::begin:一个喷漆装置一次只能由一个喷漆工完成。

我不知道错误可能是由于哪里。整个项目都在这里

参数是:

./htmltopdf http://stackoverflow.com destinationFolder

(destinationFolder尚未实现,必须直接修改源代码)

你的代码中有多个Painter。搜索它。我想问题出在你的"打印机课"上。对于printPDF方法中的本地打印机,它可以工作。试一试:

void webview::printPDF(bool ok)
{
    QPrinter printer(QPrinter::HighResolution);
    QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
        QString(), "*.pdf");
    printer.setPageSize(QPrinter::A4);
    printer.setOrientation(QPrinter::Portrait);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    page()->mainFrame()->print(&printer);
}

更新:这里有一个小的工作示例:

testview.cpp

#include "testview.h"
#include <QWebFrame>
#include "webview.h"
#include <QWebPage>
#include <QGridLayout>
testview::testview(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    QGridLayout* Layout = new QGridLayout(this);
    webview* w = new webview(this);
    w->setMinimumSize(500, 500);
    centralWidget()->setLayout(Layout);
    bool a = connect(w->page()->mainFrame(), SIGNAL(loadFinished(bool)), w, SLOT(printPDF(bool)));
    w->page()->mainFrame()->load(QUrl("http://stackoverflow.com"));
}
testview::~testview()
{
}

webview.cpp

#include "webview.h"
#include <QPrinter>
#include <QWebFrame>
#include <QFileDialog>
webview::webview(QWidget *parent)
    : QWebView(parent)
{
}
webview::~webview()
{
}
void webview::printPDF(bool ok)
{
    QPrinter printer(QPrinter::HighResolution);
    QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
        QString(), "*.pdf");
    printer.setPageSize(QPrinter::A4);
    printer.setOrientation(QPrinter::Portrait);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    page()->mainFrame()->print(&printer);
}

问题在别处:

在我的main.cpp中,我有这个:

QObject::connect(nav->getFrame(), SIGNAL(loadFinished(bool)), nav, SLOT(printPDF(bool)));
[...]
delete args;
delete pdf;
delete nav;
return app.exec();

app.exec()是一种无限循环,它处理Qt事件,从而允许程序不关闭。如果我在调用这个函数之前继续删除,那么我将有新释放的指针,这些指针将被使用

If I Do:

bool result = app.exec();
delete args;
delete pdf;
delete nav;
return result;