Need Help : how to paint qdeclarativeItem into qpixmap or qimage?
I tried to paint one qdeclarativeitem into a qpixmap. So I tried the code below
- class MyItem : public QDeclarativeItem
- {
- ............
- }
- {
- QStyleOptionGraphicsItem styleOption;
- this->paint(&painter, &styleOption, 0);
- return image;
- }
then I want to draw the image from another qdeclaraticeitem
- class MyGallery : public QDeclarativeItem
- {
- {
- Q_UNUSED(option);
- painter->drawImage(0, 0, sourceImage);
- }
- sourceImage = image;
- }
- }
but I can get only one black picture in the qmlviewer, I mean MyGallery only draw a black rectangle, can somebody tell me why and how to solve this?
2 replies
If you use QGLWidget for render, you can use my method view-graber.cpp [git.fedorahosted.org]
C++
- void ViewGraber::takeSnapshot() {
- //QDeclarativeItem *parent = parentItem();
- if (!_delegate)
- return;
- scheduledSnapshot = true;
- paintSnapshot = true;
- update(_delegate->boundingRect());
- }
- void ViewGraber::releaseSnapshot() {
- paintSnapshot = false;
- }
- void ViewGraber::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0) {
- Q_UNUSED(option);
- if (!paintSnapshot)
- return;
- //QDeclarativeItem *parent = parentItem();
- if (!_delegate)
- return;
- if (scheduledSnapshot) {
- if (!qgl)
- return;
- _delegate->paint(painter, option, widget);
- scheduledSnapshot = false;
- snapshot = qgl->grabFrameBuffer(false);
- qDebug() << "MyWebView: take snapshot ";
- emit snapshotTaken();
- }
- //rect = this->scene()->mapToItem(this, rect);
- //qDebug() << "MyWebView: paint" << sceneMapRect << selfMapRect;
- painter->drawImage((sceneMapRect.x() - selfMapRect.x()) *-1, (sceneMapRect.y() - selfMapRect.y()) *-1, snapshot);
- }
in QML
- ViewGraber{
- id: graber
- opacity: 1 // neded for infoking paint method
- delegate: webView // component for screenshot
- }
JavaScript
- graber.takeSnapshot();
You must log in to post a reply. Not a member yet? Register here!


