[SOLVED] QWebPage / QPrinter - can't load page
-
Hello every body,
I am new in Qt and I need to do a program who export html page to PDF
So, the main idea is use QWebPage for interpret html and export itself to pdf with QPrinter.
I have two class webview who use QWebPage and Print who use QPrinter.
In main.cpp I have connect LoadFinished to PrintPDF slot:
@ 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, "%s\n", 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); }
@
This is what my console display:
bq. non okay
QPainter::begin: A paint device can only be painted by one painter at a time.I noticed with a Local Printer in the printPDF Method it works (but the page has been printed is a blank page)
I have no idea where the error might be due. The whole project is here : http://paf.im/9zSsL
The arguments are:
@ ./htmltopdf http://qt-project.org destinationFolder@
(destinationFolder is not yet implemented, you must directly modify the source code)
-
The problem was elsewhere :
In my main.cpp I had this :
@QObject::connect(nav->getFrame(), SIGNAL(loadFinished(bool)), nav, SLOT(printPDF(bool)));
[...]
delete args;
delete pdf;
delete nav;return app.exec();@
app.exec() is a kind of infinit loop who thaht handles Qt events and thus allows the program does not close. If I proceed to delete before calling this function,then I will have freshly deallocated pointers that will be used
If I Do :
@bool result = app.exec();
delete args;
delete pdf;
delete nav;return result;@
It's work fine !