在 QML 中截取特定项目的屏幕截图的方法是什么? [英] What would be the way to take a screenshot of a particular Item in QML?

查看:23
本文介绍了在 QML 中截取特定项目的屏幕截图的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在 QML 中截取整个窗口的屏幕截图.

我在 QML 窗口中有一个 Video 元素.该视频显示在 Rectangle 中.

截取那个矩形而不是整个窗口的屏幕截图的方法是什么?

解决方案

看看 ItemgrabToImage 方法.

boolgrabToImage(callback, targetSize)

<块引用>

将项目抓取到内存中的图像中.

抓取异步发生,JavaScript 函数回调抓取完成时调用.

使用 targetSize 指定目标图像的大小.默认,结果将与项目具有相同的大小.

如果无法启动抓取,则函数返回 false.

以下代码段显示了如何抓取项目并存储结果到一个文件.

矩形{编号:来源宽度:100高度:100梯度:梯度{GradientStop { 位置:0;颜色:钢蓝色"}GradientStop { 位置:1;颜色:黑色" }}}//...source.grabToImage(函数(结果){result.saveToFile("something.png");});

<块引用>

以下代码段展示了如何抓取一个项目并使用结果另一个图像元素.

图片{编号:图像}//...source.grabToImage(函数(结果){image.source = result.url;},Qt.size(50, 50));

<块引用>

注意:此函数会将项目渲染到屏幕外的表面并将该表面从 GPU 的内存复制到 CPU 的内存中,可能相当昂贵.对于实时"预览,请使用图层或着色器效果源.

这也适用于 QtQuick 2.0.

I know how to take a screenshot of the whole window in QML.

I have a Video element in the QML window. That video is shown in a Rectangle.

What would be the way to take a screenshot of that Rectangle rather than the whole window?

解决方案

Take a look at Item's grabToImage method.

bool grabToImage(callback, targetSize)

Grabs the item into an in-memory image.

The grab happens asynchronously and the JavaScript function callback is invoked when the grab is completed.

Use targetSize to specify the size of the target image. By default, the result will have the same size as the item.

If the grab could not be initiated, the function returns false.

The following snippet shows how to grab an item and store the results to a file.

Rectangle {
    id: source
    width: 100
    height: 100
    gradient: Gradient {
        GradientStop { position: 0; color: "steelblue" }
        GradientStop { position: 1; color: "black" }
    }
}
    // ...
    source.grabToImage(function(result) {
                           result.saveToFile("something.png");
                       });

The following snippet shows how to grab an item and use the results in another image element.

Image {
    id: image
}
    // ...
    source.grabToImage(function(result) {
                           image.source = result.url;
                       },
                       Qt.size(50, 50));

Note: This function will render the item to an offscreen surface and copy that surface from the GPU's memory into the CPU's memory, which can be quite costly. For "live" preview, use layers or ShaderEffectSource.

This works with QtQuick 2.0 too.

这篇关于在 QML 中截取特定项目的屏幕截图的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆