I have application whith whidgets on mainform. I need to render one of widgets (qwtPlot) to QPixmap using QGraphicsScene. I use such code for that:
painter.fillRect(0, 0, 300, 300, Qt::black);
QGraphicsProxyWidget
*widget
= scene
->addWidget
(qwtPlot);
qwtPlot->repaint();
qwtPlot->show();
scene
->render
(&painter,
QRectF(),
QRect(), Qt
::IgnoreAspectRatio);
if (pixmap.
save(QApplication::applicationDirPath() + "/image.png",
"PNG")) {
box.setText("Saved");
box.exec();
}
delete widget;
delete scene;
QPixmap pixmap(300, 300);
QPainter painter(&pixmap);
painter.fillRect(0, 0, 300, 300, Qt::black);
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
qwtPlot->repaint();
qwtPlot->show();
scene->render(&painter, QRectF(), QRect(), Qt::IgnoreAspectRatio);
if (pixmap.save(QApplication::applicationDirPath() + "/image.png", "PNG"))
{
QMessageBox box;
box.setText("Saved");
box.exec();
}
delete widget;
delete scene;
To copy to clipboard, switch view to plain text mode
But I haven't any whidgets in "image.png" file - all file is black. Why my widget don't show in the file?
Bookmarks