使用Qt5拍摄完整桌面的屏幕截图 [英] Taking Screenshot of Full Desktop with Qt5

查看:948
本文介绍了使用Qt5拍摄完整桌面的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个随附的示例中弄清楚了如何使用Qt5截取今天的桌面屏幕快照,该示例获取主屏幕,抓取然后保存。

I figured out how to take a screenshot of the desktop today with Qt5 from an included example which gets the primary screen, grabs it, and then saves it.

我是在未经测试的情况下从Python转换代码,所以如果有一个小的语法错误,那么您知道的。因此,我可以使用以下命令轻松地获取主屏幕的屏幕截图:

I'm translating the code from Python without testing so if there's a small syntax error, then yeah you know. So I can easily take a screenshot of the primary screen with:

QApplication a(argv, argc);

QScreen *screen = a.primaryScreen();

QPixmap screenshot = screen->grabWindow(0);

screenshot.save('screenshot.png', 'png');

(显然)这将截取主监视器的屏幕截图。问题是我需要对所有监视器进行截图。所以我想出了这个:

This will (obviously) take a screenshot of the primary monitor. The problem is I need to take a screenshot of all of the monitors. So I came up with this:

QList<QScreen*> screens = a.screens();
QScreen *screen;
QPixmap screenshot;

for(int i = 0; i < screens.length(); i++){
    screen = screens.at(i);
    screenshot = screen->grabWindow(0);
    screenshot.save(QString::number(i) + ".png", 'png');
}
//takes and saves two screenshots on my end

此结果我的两个监视器都可以,但是保存的图像都是主监视器的屏幕截图,我不知道该如何获取其他监视器。我已经玩了几个小时了,仍然无法弄清楚。有人可以帮我吗?

This finds both of my monitors but the saved images are all a screenshot of the primary monitor and I can't figure out how to get the others. I've been playing with this for a few hours now and still can't figure it out. So can someone help me out?

推荐答案

我找到了解决此问题的简单方法。最近查看文档时,我发现'getWindow'方法的默认参数为

I figured out a simple fix for this problem. When I was looking through the documentation recently, I found that the 'getWindow' method had default parameters of

(x = 0, y = 0, width = -1, height = -1)

所以无论我叫什么屏幕使用getWindow方法时,它始终为我提供相同的几何形状。因此,要解决此问题,方法很简单:

So no matter what screen I called the getWindow method with, it kept giving me the same geometry. So to fix this, it's simply:

//Screen geometry
QRect g = screen->geometry();

//Take the screenshot using the geometry of the screen
QPixmap screenShot = screen->grabWindow(0, g.x(), g.y(), g.width(), g.height());

这篇关于使用Qt5拍摄完整桌面的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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