PDA

View Full Version : Saving a FULLY rendered QWidget on a graphics scene



technoViking
25th October 2010, 15:06
Hi,

I have several widgets in a graphics scene. I want to take a image copy of 1 of the widgets.

I attempted the following:



QPixmap screenshot;
screenshot = QPixmap::grabWidget(theWidget,theWidget.rect());
screenshot.save("test.png","PNG");


THis will save the widget but the widget is stripped of EVERYHTING. Basically you don't see the background image on the widget, you don't see any alpha settings I set, you simply see an outline of the widget and some text.

I also tried:


QImage image(300,300,QImage::Format_RGB32);
QPainter painter(&image);

TheGraphicsView->render(&painter);
image.save("test.png","PNG");


This will give me the exact same result as above.


Lastly I tried:


screenshot = QPixmap::grabWindow(QApplication::desktop()->winId());


This will capture the whole screen which is not what I wanted. This will though show everyhting fully rendered.

Any ideas how I can capture just 1 WIDGET in the graphics view fully rendered?

Thanks

wysota
25th October 2010, 16:34
QPixmap px(QRect(QPoint(0,0), theWidget->sceneRect().size());
QPainter painter(&px);
scene->render(&painter, QRectF(), theWidget->sceneRect());

technoViking
25th October 2010, 18:00
Hi thanks for the responce! But QWdiget doesn't have a member sceneRect() it does have a ->rect() function though, is that what you meant?



QPixmap px(QRect(QPoint(0,0), mpCalendarWidget->rect().size()));
QPainter painter(&px);
this->escapeGraphicsScene->render(&painter,QRectF(),mpCalendarWidget->rect().size());


I get an error saying:
error: no matching function for call to QPixmap::Qpixmap(QRect)

Another error saying:
No matching function call to QGraphicsScence::render(QPainter*,QRectF,QSize)

The error is on the first line, I guess the constructor for QPixmap dosen't support QRect.


EDIT:
I changed the code to the following:



QPixmap px(mpCalendarWidget->rect().size());
QPainter painter(&px);
this->TheGraphicsScene->render(&painter,QRectF(),mpCalendarWidget->rect());

QLabel *mpImageLabel = new QLabel(this);
mpImageLabel->setPixmap(px);
mpImageLabel->show();


It compiles but when I run the application nothing is shown except a black box in the corner of the screen. Am I doing something wrong?

Thanks

wysota
25th October 2010, 19:16
Is "mpCalendarWidget" inserted into the scene or is it just some widget floating over the view?

technoViking
25th October 2010, 21:21
Hi,

yes its in the scene, I have code here:



GraphicsScene = new QGraphicsScene( QRect( 0, 0, GraphicsView->width(), GraphicsView->height()) );

GraphicsView->setScene(GraphicsScene);

mpCalendarWidget = new CldrWidget(mpStateMachine);

mpCalendarWidgetProxy = GraphicsScene->addWidget(mpCalendarWidget);
mpCalendarWidget->setGeometry((gLCDhres-mpCalendarWidget->width())/2,
mpMainButtonTray->y()-mpCalendarWidget->height(),
mpCalendarWidget->width(),
mpCalendarWidget->height());

wysota
25th October 2010, 21:30
So "mpCalendarWidget" from my code is "mpCalendarWidgetProxy" from yours.

technoViking
25th October 2010, 21:58
Hi,

Thanks for the responce! I think we almost got it! But still not able to compile what you gave or perhaps I'm misunderstanding.
So I attempted:



QPixmap px(QRect(QPoint(0,0),mpCalendarWidgetProxy->sceneBoundingRect().size() )); //note: ProxyWidget doesn't have a member called scenceRect so I figured you meant sceneBoundingRect

QPainter painter(&px);
this->GraphicsScene->render(&painter,QRectF(),mpCalendarWidgetProxy->sceneBoundingRect()); //note: also used boundingRect here as well since no scenceRect could be found.


I got the follow error:
no matching function call to 'QRect::QRect(QPoint,QSizeF)'
So it looks like this line: QPixmap px(QRect(QPoint(0,0),mpCalendarWidgetProxy->sceneBoundingRect().size() )); is the issue

wysota
25th October 2010, 22:47
QSizeF::toSize()