Qt Forum

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

    Save QQuickView screen to image

    QML and Qt Quick
    2
    6
    3346
    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.
    • U
      utcenter last edited by

      I am trying to save a QQuickView as an image but with no success. I tried the obvious approach:

      @view->renderTarget()->toImage().save(fileName);@

      However this crashes, and the reason for this is probably that renderTarget() returns a null pointer.

      Am I doing something wrong? Or maybe there is a better way?

      1 Reply Last reply Reply Quote 0
      • M
        melghawi last edited by

        Try:

        @
        QImage image = view->grabWindow();
        @

        This function might not work if the window is not visible.
        "QQuickWindow::grabWindow()":http://qt-project.org/doc/qt-5.0/qtquick/qquickwindow.html#grabWindow

        1 Reply Last reply Reply Quote 0
        • U
          utcenter last edited by

          Thanks, this works, strange why the other method doesn't.

          Anyway, what if I want to save only a particular QML object as an image, would that be possible?

          1 Reply Last reply Reply Quote 0
          • M
            melghawi last edited by

            I'm not sure if there is a direct way to do this but you can grab the entire window and then cut out the bit you want.

            Try this:

            @
            QQuickWindow *window = item->window();
            QImage grabbed = window->grabWindow();
            QRectF rf(item->x(), item->y(), item->width(), item->height());
            rf = rf.intersected(QRectF(0, 0, grabbed.width(), grabbed.height()));
            QImage itemImage = grabbed.copy(rf.toAlignedRect());
            @

            Read more "here":http://qt-project.org/forums/viewthread/18371.

            1 Reply Last reply Reply Quote 0
            • U
              utcenter last edited by

              Yeah that could work in a limiter range of cases, but it won't work for nested and layered structures. I'd rather be able to get the objects pixmap from the final stage of the framebuffer before it is blended and composed in the scene.

              Plus, the grabWindow() method is brutally slow and even in the doc is labeled as a performance problem.

              1 Reply Last reply Reply Quote 0
              • M
                melghawi last edited by

                Yes that is true. You can get a texture from a "QQuickItem":http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html only if it is a "textureProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html#textureProvider. At a quick glance, there doesn't seem to be anything that exposes an objects' pixmap.

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