November 7, 2011

hailong hailong
Lab Rat
22 posts

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

  1. class MyItem : public QDeclarativeItem
  2. {
  3.     ............
  4.     QImage getSnapShot();
  5. }
  6.  
  7. QImage MyItem::getSnapShot()
  8. {
  9.     QImage image(this->boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
  10.     image.fill(QColor(0, 0, 0).rgb());
  11.     QPainter painter(&image);
  12.     QStyleOptionGraphicsItem styleOption;
  13.     this->paint(&painter, &styleOption, 0);
  14.     return image;
  15. }

then I want to draw the image from another qdeclaraticeitem

  1. class MyGallery : public QDeclarativeItem
  2. {
  3.         void paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0)
  4.     {
  5.         Q_UNUSED(option);
  6.         painter->drawImage(0, 0, sourceImage);
  7.     }
  8.     void setImage(const QImage &image) {
  9.         sourceImage = image;
  10.     }
  11. }

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

November 8, 2011

Jens Jens
Hobby Entomologist
137 posts

Well based on your provided snippets, I don’t see you ever actually calling setImage or getSnapshot so I would really just expect a black rectangle there. :)

December 20, 2011

karry karry
Lab Rat
1 posts

If you use QGLWidget for render, you can use my method view-graber.cpp [git.fedorahosted.org]

C++

  1. void ViewGraber::takeSnapshot() {
  2.   //QDeclarativeItem *parent = parentItem();
  3.   if (!_delegate)
  4.     return;
  5.  
  6.   scheduledSnapshot = true;
  7.   paintSnapshot = true;
  8.  
  9.   setFlag(QGraphicsItem::ItemHasNoContents, false);
  10.   update(_delegate->boundingRect());
  11. }
  12.  
  13. void ViewGraber::releaseSnapshot() {
  14.   paintSnapshot = false;
  15. }
  16.  
  17. void ViewGraber::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0) {
  18.   Q_UNUSED(option);
  19.  
  20.   if (!paintSnapshot)
  21.     return;
  22.  
  23.   //QDeclarativeItem *parent = parentItem();
  24.   if (!_delegate)
  25.     return;
  26.  
  27.   if (scheduledSnapshot) {
  28.     QGLWidget *qgl = reinterpret_cast<QGLWidget *> (widget);
  29.     if (!qgl)
  30.       return;
  31.  
  32.     _delegate->paint(painter, option, widget);
  33.     scheduledSnapshot = false;
  34.     snapshot = qgl->grabFrameBuffer(false);
  35.     qDebug() << "MyWebView: take snapshot ";
  36.     emit snapshotTaken();
  37.   }
  38.   QPointF sceneMapRect = _delegate->mapToScene(0, 0);
  39.   QPointF selfMapRect = ((QGraphicsItem*)_delegate)->mapToItem((QGraphicsItem*) this, QPointF(0, 0));
  40.   //rect = this->scene()->mapToItem(this, rect);
  41.   //qDebug() << "MyWebView: paint" << sceneMapRect << selfMapRect;
  42.   painter->drawImage((sceneMapRect.x() - selfMapRect.x()) *-1, (sceneMapRect.y() - selfMapRect.y()) *-1, snapshot);
  43. }

in QML

  1. ViewGraber{
  2.  id: graber
  3.  opacity: 1 // neded for infoking paint method
  4.  delegate: webView // component for screenshot
  5. }

JavaScript

  1.  graber.takeSnapshot();

 
  ‹‹ How to display the avatar/picure associate with a contact in qml.      retrieving text messages from inbox ››

You must log in to post a reply. Not a member yet? Register here!