Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Application crahes on QWebEnginePage printing attempt
QtWS25 Last Chance

Application crahes on QWebEnginePage printing attempt

Scheduled Pinned Locked Moved Solved QtWebEngine
printing
3 Posts 2 Posters 769 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.
  • A Offline
    A Offline
    abduunn
    wrote on 7 Jan 2020, 17:50 last edited by abduunn 1 Jul 2020, 19:37
    #1

    Hello everyone

    I am trying to use the print method of QWebEnginePage with the following code. The application crashes and I can't see where the problem is:

        QPrinter printer;
        QPrintDialog *pDlg = new QPrintDialog(&printer, this);
        QWebEngineView *view = new QWebEngineView(this);
        view->setHtml("<h1>Hello</h1>", QUrl("about:blank"));
    
        if (pDlg->exec()) {
            view->page()->print(&printer, [=](bool) {});
        }
    

    I tried to run it under debugging and it seems to crash on QWin32PrintEngine.
    I also tried to use the printToPdf method and it exports a pdf file just fine.

    The same method is used to print a QTextEdit widget contents using the same printer:

            QPrinter printer;
            QPrintDialog *pDlg = new QPrintDialog(&printer, this);
            QTextEdit *tedit = new QTextEdit("Hello");
            if (pDlg->exec()) {
                tedit->print(&printer);
            }
    

    What am I doing wrong?
    Thank you all in advance.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Jan 2020, 17:54 last edited by
      #2

      Hi and welcome to devnet,

      You are not checking that the call to page returns a valid pointer.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        abduunn
        wrote on 7 Jan 2020, 19:35 last edited by
        #3

        @SGaist said in Application crahes on QWebEnginePage printing attempt:

        checking that the call to page returns a valid pointer.

        Thank you.

        With the clue I had from this response, and with the help of demobrowser example which is bundled with Qt-5.6 I rewrote the code and could send the page to printer successfully.

        the code after the modification:

                QWebEngineView *view = new QWebEngineView(this);
                view->setHtml("<h1>Hello</h1>", QUrl("about:blank"));
                printRequest(view->page());
        

        the functions are defined as following

        
        void MainWindow::slotHandlePagePrinted(bool result)
        {
            Q_UNUSED(result);
        
            delete printer;
            printer = nullptr;
        }
        
        void MainWindow::printRequest(QWebEnginePage *page)
        {
            if (printer)
                return;
            printer = new QPrinter();
            QScopedPointer<QPrintDialog> dialog(new QPrintDialog(printer, this));
            if (dialog->exec() != QDialog::Accepted) {
                slotHandlePagePrinted(true);
                return;
            }
            else {
                page->print(printer, [=](bool) {});
            }
        }
        

        and in the header file:

        private slots:
            void slotHandlePagePrinted(bool result);
        
        private:
            QPrinter *printer;
            void printRequest(QWebEnginePage *page);
        
        1 Reply Last reply
        0

        2/3

        7 Jan 2020, 17:54

        • Login

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