Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How to capture an QML child element to an png?



  • 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.



  • 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.



  • You need to wait for Qt 5.3



  • 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?



  • Hello,

    Have you found the solution for this issue ?



  • it is my problem too...



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



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



  • Yes, The solution has just appeared in Qt 5.4



  • Yes, The solution has just appeared in Qt 5.4



  • and how?



  • and how?


  • Moderators

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


  • Moderators

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


Log in to reply