Search code examples
c++qtqpainterqimage

Qt Cannot convert QPaintDevice to QImage


This sounds like silly, but I cannot take a screenshot of a QwebView.

QImage image(view.page()->viewportSize(), QImage::Format_ARGB32);
QPainter painter;
painter.begin(image); // Here is the error. See below
view.page()->mainFrame()->render(&painter);
painter.end();
image.save("out.png");

And the error msg is,

mainwindow.cpp:115: error: no matching function for call to 'QPainter::begin(QImage&)'
candidate is bool QPainter::begin(QPaintDevice*)

What I know is ,QPaintDevice is the base of QPaint.


Solution

  • Looks from the error that you need to pass image by pointer:

    painter.begin(&image);