Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Need Help : how to paint qdeclarativeItem into qpixmap or qimage?

    QML and Qt Quick
    3
    3
    2951
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hailong last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • J
        Jens last edited by

        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. :)

        1 Reply Last reply Reply Quote 0
        • K
          karry last edited by

          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();@

          1 Reply Last reply Reply Quote 0
          • First post
            Last post