qml 截屏功能

qml 截屏主要使用QQuickWindow中grabWindow()完成,关于grabWindow()介绍如下亦可查看帮助所以不多说。

QImage QQuickWindow::grabWindow()
Grabs the contents of the window and returns it as an image.

It is possible to call the grabWindow() function when the window is not visible. This requires that the window is created and has a valid size and that no other QQuickWindow instances are rendering in the same process.

Warning: Calling this function will cause performance problems.

Warning: This function can only be called from the GUI thread.

qml的截屏需要Qt+qml混编。

首先在main.cpp中注册qml中需要用到的类

....setContextProperty("robotData", robotData);

CPP中函数的实现

bool RobotData::screenshot(QQuickWindow *quic)//参数为截屏窗口的ID
{
    image=quic->grabWindow();//截屏
    QMatrix matrix;
    matrix.rotate(270);
    image=image.transformed(matrix);//因为设备是竖屏,但截出来的图片是横向显示的,这样看起来很难受,所以先进行一下图片旋转。
    QString str="Screenshot";
    image.save(str, "PNG");//将截屏保存为PNG图片,命名为“Screenshot.png”。
    return true;
}

接下来写qml

ApplicationWindow {
    id:root

    ..{

       if(robotData.screenshot(root)){console.log("OK");}//只需在需要的地方加入调用就可以了,root为要截屏窗口的ID。

}

}

以上就完成了,qml窗口的截屏,简单实用。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值