Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QWebPage / QPrinter - can't load page

[SOLVED] QWebPage / QPrinter - can't load page

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 991 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nymeria
    wrote on last edited by
    #1

    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&#40;args->getSource(&#41;&#41;&#41;;
        } 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)

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nymeria
      wrote on last edited by
      #2

      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 !

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved