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
{
............
QImage getSnapShot();
}QImage MyItem::getSnapShot()
{
QImage image(this->boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
image.fill(QColor(0, 0, 0).rgb());
QPainter painter(&image);
QStyleOptionGraphicsItem styleOption;
this->paint(&painter, &styleOption, 0);
return image;
}
@then I want to draw the image from another qdeclaraticeitem
@
class MyGallery : public QDeclarativeItem
{
void paint(QPainter painter, const QStyleOptionGraphicsItem option, QWidget* widget = 0)
{
Q_UNUSED(option);
painter->drawImage(0, 0, sourceImage);
}
void setImage(const QImage &image) {
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?
-
If you use QGLWidget for render, you can use my method "view-graber.cpp":http://git.fedorahosted.org/git/?p=makneto.git;a=blob;f=src/ui-mobile/view-graber.cpp;hb=refs/heads/karry
C++
@void ViewGraber::takeSnapshot() {
//QDeclarativeItem *parent = parentItem();
if (!_delegate)
return;scheduledSnapshot = true;
paintSnapshot = true;setFlag(QGraphicsItem::ItemHasNoContents, false);
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) {
QGLWidget *qgl = reinterpret_cast<QGLWidget *> (widget);
if (!qgl)
return;_delegate->paint(painter, option, widget); scheduledSnapshot = false; snapshot = qgl->grabFrameBuffer(false); qDebug() << "MyWebView: take snapshot "; emit snapshotTaken();
}
QPointF sceneMapRect = _delegate->mapToScene(0, 0);
QPointF selfMapRect = ((QGraphicsItem*)_delegate)->mapToItem((QGraphicsItem*) this, QPointF(0, 0));
//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();@