Want to have a common method to save the PNG of QMainWindow ,QGraphicsView. Qwidget
-
I went through https://forum.qt.io/topic/75776/how-to-take-snapshot-of-the-whole-gui/5
I am not able to have common methodology to save the screenshot of QMainWindow , QGraphicsView and QWidget
Can somesuggest what is common way to save the methodlogy -
You get the screenshot in form of a QImage. This you can save in multple formats (incl. PNG) with the
save
function: http://doc.qt.io/qt-5/qimage.html#reading-and-writing-image-files-Michael.
-
my Question is how to get QImage from object of
- QMainWindow
- QWidget
- QGraphicsView
-
You can use one of the
render
functions of QWidget: http://doc.qt.io/qt-5/qwidget.html#render There you can use a QImage as QPaintDevice argument.-Michael.
-
this is not working for QGraphicsView version 4.3.3
QPixmap * pixmap = new QPixMap(this->size())
this render(pixmap)
pixmap->save(fileName)does not work for QGraphicsView in 4.3.3 . Could you please comment
-
What does not work? What does it do that is not correct?
-
I am getting compilation error
class myView: plublic QGraphicsView {
}
no matching function call for myView::render(QPixMap* &)
-
Maybe a typo: QPixMap -> QPixmap
-
no even if fix typo it is issue
Could you please try ant your end where I can common method to save snapshot of QMainWindow ,QWidget amd QGraphicsView and a sample sample code will be helpful
-
QPixmap *px = new QPixmap(this->size());
this->render(pixmap);
pixmap->save(file) not working for QGraphicsview but working for QMainWindow and QWidgetfor QGraphicsView , render not found and signature should be QGraphicsView::render(QPainter
-
Hi,
Do you have "#include <QPixmap>" in your .cpp file ?
-
Yes it is included
-
Can you show the complete code ?
-
@Qt-Enthusiast said in Want to have a common method to save the PNG of QMainWindow ,QGraphicsView. Qwidget:
QPixmap *px = new QPixmap(this->size());
this->render(pixmap);
pixmap->save(file) not working for QGraphicsview but working for QMainWindow and QWidgetfor QGraphicsView , render not found and signature should be QGraphicsView::render(QPainter
I think this is because the render method is overloaded in QGraphicsView, but only for QPainter*, not for QPaintDevice*.
You can easily solve that problem by creating your own painter like so:
{ QPainter painter; QPixmap pixmap(sizeX,sizeY); painter.begin(&pixmap); this->render(&painter); painter.end(); pixmap.save(file); }
-
@Asperamanca the render method using QPaintDevice is inherited from QWidget so it looks like there's something else at play.render is not virtual so it will be hidden by the new version of the derived class.
[edit: fixed comment SGaist]
-
@SGaist said in Want to have a common method to save the PNG of QMainWindow ,QGraphicsView. Qwidget:
@Asperamanca the render method using QPaintDevice is inherited from QWidget so it looks like there's something else at play.
When you have two overloads of a method in the base class, and reimplement one of them in a derived class, you will (by default) no longer be able to call the other one. Unless you allow it through the 'using' keyword. I don't see QGraphicsView doing that.
-
@Asperamanca absolutely correct, I've forgotten that render wasn't a virtual method.