qt截图怎样实现橡皮擦_使用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.

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

解决方案

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)

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());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值