Application crahes on QWebEnginePage printing attempt
-
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. -
Hi and welcome to devnet,
You are not checking that the call to page returns a valid pointer.
-
@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);