Printscreen QML item C++



  • Is it possible to grab printscreen of QML item without saving it to file? I don't want to save image to file with myQmlItem.grabToImage(function(result) { result.saveToFile("myImage.png"); }); because I will use the image in underlying C++ code. Of course, I can save image to file, then read it back in C++ but it would be good if I can access the image in C++ directly without storing it and reading back.


  • Moderators

    @MartinD

    QML:

    myItem.grabToImage( function(result) {
                myObj.processQMLImage(result);
            } );
    

    C++

    void processQMLImage(QObject* imageObj)
    {
        if( QQuickItemGrabResult* item = qobject_cast<QQuickItemGrabResult*>(imageObj) )
        {
              QImage image = item->image();
              //.....
        }
    

  • Moderators

    @MartinD Or you can just do it directly by calling follwing function from QML:

    void capture(QQuickItem *item)
    {
        auto grabResult = item->grabToImage(QSize(50,50));
    
        connect(grabResult.data(), &QQuickItemGrabResult::ready, [=]() {
            QImage image = grabResult.data()->image();
        });
    }
    


  • Thanks, very useful!



  • I want grab QML item to save video. eg: mp4
    Is there a way to record the layer ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.