Results 1 to 3 of 3

Thread: Printing to pdf with QWebEnginePage print() function

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    48
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Printing to pdf with QWebEnginePage print() function

    New with Qt 5.8 is the print() functionality with the QWebEnginePage class - http://doc.qt.io/qt-5/qwebenginepage.html#print . I'm trying to port some old programs that used QWebView to print to pdf to this new approach, and I'm having trouble understanding the second parameter required for the print function. The documentation says it is expecting a FunctorOrLambda resultCallback. I'm new-ish to both Qt and C++, so I have yet to fully get my head around the concept of functors or lambdas. That I can learn, but first I'm just trying to grasp the overall purpose of the second parameter of print and what exactly is expected. Based on the documentation and this blogpost, am I understanding correctly that the print function is looking for some reference to a function that makes sure the printer stays valid until printing is successful? If so, any hints on how I might approach that in the below code structure that I am already working with?
    Qt Code:
    1. QWebEngineView *view = new QWebEngineView();
    2. view->setHtml(htmlString);
    3.  
    4. QPrinter pdfPrinter(QPrinter::HighResolution);
    5.  
    6. view->page()->print(&pdfPrinter, HELP_REQUIRED_HERE);
    To copy to clipboard, switch view to plain text mode 

    Anything to help me understand would be greatly appreciated!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Printing to pdf with QWebEnginePage print() function

    This kind of callback is how the web environment handles the asynchronous nature of operations.

    Basically you launch an operation, in this case print, and provide a function that is called when the operation finished, e.g. to provide you with feedback on its success or the result, etc.

    In this case the function is to report success or failure, i.e. the function required here needs to have a boolen argument and it will be called with "true" if the printing succeeded.

    A lambda is an inline defined piece of code that can be called like function and can take values and reference from the environment of the code where it appears.
    A functor is basically the same just with different (older) language features. Lambdas require a C++11 capable build environment.

    Independent of that you'll need to make sure that the printer is valid past the call to print, due to the asynchronous nature of the operations.

    If you don't need explicit feedback on the operation you could look into the printToPdf method that takes a filename.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ce_nort (9th February 2017)

  4. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    48
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Printing to pdf with QWebEnginePage print() function

    Thanks for that overview, it was very helpful. I spent sometime yesterday with functors and lambdas, so I think I have a basic grasp on them now. I don't really need explicit feedback on the operation, but I also encountered a lot of trouble when trying to get the functionality I wanted out of the printToPdf method (this was several months ago, so I can't recall the exact issues I encountered). Basically what I'm trying to do is save some html as a PDF and then immediately open the saved pdf so that the user may print it if desired. Maybe I'll revisit printToPdf and see if it's a better solution for my needs. Either way, thanks for the break down!

Similar Threads

  1. Is QWebEnginePage::setHtml() synchronous or Asynchronous?
    By ayanda83 in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2017, 10:42
  2. QWebView print() not printing as WYSIWYG
    By thiagoalencar22 in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 13:43
  3. My own print function in Qt Script
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 18th February 2009, 13:50
  4. printing with QwtPlot::print
    By user in forum Qwt
    Replies: 1
    Last Post: 7th March 2008, 08:56
  5. Replies: 0
    Last Post: 28th June 2006, 21:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.