Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Need Help : how to paint qdeclarativeItem into qpixmap or qimage?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 3.1k Views
  • 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 Offline
    H Offline
    hailong
    wrote on last edited by
    #1

    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
    0
    • J Offline
      J Offline
      Jens
      wrote on last edited by
      #2

      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
      0
      • K Offline
        K Offline
        karry
        wrote on last edited by
        #3

        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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved