Qt Forum

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

    How to capture an QML child element to an png?

    QML and Qt Quick
    6
    14
    6601
    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.
    • V
      VTiTux last edited by

      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 Reply Quote 0
      • V
        VTiTux last edited by

        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 Reply Quote 0
        • V
          Vincent007 last edited by

          You need to wait for Qt 5.3

          1 Reply Last reply Reply Quote 0
          • V
            VTiTux last edited by

            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 Reply Quote 0
            • T
              Tân Ngọc Đỗ last edited by

              Hello,

              Have you found the solution for this issue ?

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

                it is my problem too...

                1 Reply Last reply Reply Quote 0
                • D
                  David Stiel last edited by

                  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 Reply Quote 0
                  • D
                    David Stiel last edited by

                    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 Reply Quote 0
                    • T
                      Tân Ngọc Đỗ last edited by

                      Yes, The solution has just appeared in Qt 5.4

                      1 Reply Last reply Reply Quote 0
                      • T
                        Tân Ngọc Đỗ last edited by

                        Yes, The solution has just appeared in Qt 5.4

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

                          and how?

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

                            and how?

                            1 Reply Last reply Reply Quote 0
                            • JKSH
                              JKSH Moderators last edited by

                              [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 Reply Quote 0
                              • JKSH
                                JKSH Moderators last edited by

                                [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 Reply Quote 0
                                • First post
                                  Last post