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. How to capture an QML child element to an png?
Forum Updated to NodeBB v4.3 + New Features

How to capture an QML child element to an png?

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 6 Posters 7.4k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    VTiTux
    wrote on last edited by
    #1

    Hi,
    I see this "method: ":http://stackoverflow.com/questions/11011391/saving-qml-image
    but it doesn't work.
    I substitue QDeclarativeItem by QQuickItem:
    @
    void Capturer::save(QQuickItem *item)
    {
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QStyleOptionGraphicsItem option;
    item->paint(&painter, &option, NULL);
    pix.save("/path/to/output.png");
    }
    @

    but QQuickItem hasn't got an 'paint' method.
    So, I use the QQuickPaintedItem class:

    @
    void Capturer::save(QQuickItem *item)
    {
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QQuickPaintedItem paintedItem(item);
    paintedItem.paint(&painter);
    pix.save("/path/to/output.png");
    }@

    but the error is "cannot declare variable 'paintedItem' to be of abstract type 'QQuickPaintedItem'"

    How can I to do an capture of my QML element (not the whole scene, but of an sub-element)?

    Thanks.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VTiTux
      wrote on last edited by
      #2

      I also try this "method.":http://qt-project.org/forums/viewthread/3948
      Don't work. Item is null.
      I try with QML Rectangle and QML Image as imageObj.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vincent007
        wrote on last edited by
        #3

        You need to wait for Qt 5.3

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VTiTux
          wrote on last edited by
          #4

          I have an another problem.

          With this code:
          @
          void SessionManager::test(){
          QQmlComponent component(pEngine, QUrl::fromLocalFile( "..../Test.qml" ));
          QObject *object = component.create();
          QQuickItem item = qobject_cast<QQuickItem>(object);
          QQuickItem scene = pRootObject->findChild<QQuickItem>("Window1");
          item->setParentItem(scene);
          QQuickWindow *window = item->window();
          QImage image = window->grabWindow();
          image.save( "..../test1.jpg" );
          }
          @

          • If I run this function in my main(), before "app.exec()", my view is painted but test1.jpg isn't created.

          • If I launch this function with a mouse control and a signal/slot (I use a notification icon menu to launch my development functions), (so, this function is executed later than the main() initialisation), my view is empty, but I have the jpeg file.

          My root object isn't a visible item.
          My main.qml looks like this:
          @
          Item{
          Window{
          Item{ objectName: "Window1" }
          }
          Window{
          Item{ objectName: "Window2" }
          }
          Window{
          Item{ objectName: "Window3" }
          }
          }

          @

          So, my "Test.qml" item isn't visible when it is created. But it is showed after the affectation 'setParentItem()'. (except with this problem...)
          If I remove the capture, all works fine.
          If I move my item to the window2 after the capture, it is visible:

          @
          void SessionManager::test(){
          QQmlComponent component(pEngine, QUrl::fromLocalFile( "..../Test.qml" ));
          QObject *object = component.create();
          QQuickItem item = qobject_cast<QQuickItem>(object);
          QQuickItem scene = pRootObject->findChild<QQuickItem>("Window1");
          item->setParentItem(scene);
          QQuickWindow *window = item->window();
          QImage image = window->grabWindow();
          image.save( "..../test1.jpg" );
          QQuickItem scene2 = pRootObject->findChild<QQuickItem>("Window2");
          item->setParentItem(scene2);
          }
          @
          But without that, that doesn't work.

          It is a bug with QtQuick? Or what?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tân Ngọc Đỗ
            wrote on last edited by
            #5

            Hello,

            Have you found the solution for this issue ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mehrdad
              wrote on last edited by
              #6

              it is my problem too...

              1 Reply Last reply
              0
              • D Offline
                D Offline
                David Stiel
                wrote on last edited by
                #7

                Check out "this":http://doc.qt.io/qt-5/qml-qtquick-item.html#grabToImage-method. I think it solves your problem

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  David Stiel
                  wrote on last edited by
                  #8

                  Check out "this":http://doc.qt.io/qt-5/qml-qtquick-item.html#grabToImage-method. I think it solves your problem

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tân Ngọc Đỗ
                    wrote on last edited by
                    #9

                    Yes, The solution has just appeared in Qt 5.4

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Tân Ngọc Đỗ
                      wrote on last edited by
                      #10

                      Yes, The solution has just appeared in Qt 5.4

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mehrdad
                        wrote on last edited by
                        #11

                        and how?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mehrdad
                          wrote on last edited by
                          #12

                          and how?

                          1 Reply Last reply
                          0
                          • JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            [quote author="mehrdad" date="1423500644"]and how?[/quote]Did you read David Stiel's link?

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #14

                              [quote author="mehrdad" date="1423500644"]and how?[/quote]Did you read David Stiel's link?

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              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