PDA

View Full Version : Render windget to QPixmap



giker
2nd November 2010, 07:46
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:


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;


But I haven't any whidgets in "image.png" file - all file is black. Why my widget don't show in the file?

Lykurg
2nd November 2010, 08:00
Why do you want to create a scene and add a widget to it only to render it? Isn't that pure overhead? See QPixmap::grabWidget().

giker
2nd November 2010, 08:09
I need to render my widget to QPixmap whith given size and whith same quality as on the screen. I think QGraphicsScene help me whith it.

Lykurg
2nd November 2010, 08:15
I need to render my widget to QPixmap whith given size and whith same quality as on the screen.And in contrast QPixmap::grabWidget() (and some other functions of QPixmap) does what?

giker
2nd November 2010, 08:36
Result of grabWidget() depends on current state of widget. For example, if user change mainwindow's of application sizes of child widgets will be changed. I want to get view of widget that independens on parent widget size.

Lykurg
2nd November 2010, 09:06
Ehm, yes, but you are creating a new instance before you add it to the scene via addWidget(). Instead of that simply pass it to grabWidget().

giker
2nd November 2010, 12:06
I don't uderstand you. I want know: can I render widgets that already shown at application's mainform or not? Because I can render widgets that created in code but never show at the screen:



QPixmap pixmap(300, 300);
QPainter painter(&pixmap);
painter.fillRect(0, 0, 300, 300, Qt::black);
QGraphicsScene *scene = new QGraphicsScene();
QwtPlot *qwtPlot = new QwtPlot();//this widget will show in file
QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
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;
delete qwtPlot;

Lykurg
2nd November 2010, 20:26
I want know: can I render widgets that already shown at application's mainform or not? Yes, with grabWidget(). But only in the size they are actually shown.
Because I can render widgets that created in code but never show at the screen:



QPixmap pixmap(300, 300);
QPainter painter(&pixmap);
painter.fillRect(0, 0, 300, 300, Qt::black);
QGraphicsScene *scene = new QGraphicsScene();
QwtPlot *qwtPlot = new QwtPlot();//this widget will show in file
QGraphicsProxyWidget *widget = scene->addWidget(qwtPlot);
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;
delete qwtPlot;
Yes and this is as often enough told nonsense. Use also grabWidget() for that.

Uwe
3rd December 2010, 09:27
Use QwtPlot::print - see the bode example how to use it. Also consider to use a vector graphics format like PDF or SVG.

Note that Qwt 6.x has a floating point based render engine, what gives you perfectly scalable outputs for vector graphics. Also the API for rendering graphic formats has been improved and should be more comfortable: again see the bode example.

Uwe

PS: For Qwt related questions better use the Qwt subforum. I miss most of the questions not posted there.