diff --git a/src/screenshooter.cpp b/src/screenshooter.cpp index dcb7e18..6d36ea7 100755 --- a/src/screenshooter.cpp +++ b/src/screenshooter.cpp @@ -27,6 +27,14 @@ #include #endif +static QRect virtualDesktopRect() +{ + QRect r; + for (QScreen *s : QGuiApplication::screens()) + r = r.united(s->geometry()); + return r; +} + const QImage ScreenShooter::captureFullscreen(bool captureMultipleMonitors) { QImage allMonitorsScreenshot = ScreenShooter::captureAllMonitors(); @@ -34,13 +42,10 @@ const QImage ScreenShooter::captureFullscreen(bool captureMultipleMonitors) //If capturing only a single monitor, crop the image if(!captureMultipleMonitors) { - #if QT_VERSION >= QT_VERSION_CHECK(5,10,0) - QScreen* screen = QGuiApplication::screenAt(QCursor::pos()); - #else - int screenNumber = QApplication::desktop()->screenNumber(QCursor::pos()); - QScreen* screen = QApplication::screens().at(screenNumber); - #endif - return allMonitorsScreenshot.copy(screen->geometry()); + QScreen* screen = QGuiApplication::screenAt(QCursor::pos()); + if (!screen) screen = QApplication::primaryScreen(); + QRect virt = virtualDesktopRect(); + return allMonitorsScreenshot.copy(screen->geometry().translated(-virt.topLeft())); } //Otherwise return the image as is @@ -49,9 +54,9 @@ const QImage ScreenShooter::captureFullscreen(bool captureMultipleMonitors) const QImage ScreenShooter::captureSelection(const QRect &area) { + QRect virt = virtualDesktopRect(); QImage allMonitorsScreenshot = ScreenShooter::captureAllMonitors(); - QImage areaScreenshot = allMonitorsScreenshot.copy(area); - return areaScreenshot; + return allMonitorsScreenshot.copy(area.translated(-virt.topLeft())); } const QImage ScreenShooter::captureWindow(WId windowID, bool captureWindowBorders) @@ -187,10 +192,19 @@ const QImage ScreenShooter::captureAllMonitors() } #endif //If we are on Win/Mac, or not running on Wayland, use the Qt API to take a screenshot + // Grab each screen individually and composite them into one image. INFO(QObject::tr("Capturing the screen using the Qt API")); - QScreen* screen = QApplication::primaryScreen(); - QRect screenGeometry = screen->virtualGeometry(); - QPixmap pixmap = screen->grabWindow(0, screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height()); - - return pixmap.toImage(); + QRect virt = virtualDesktopRect(); + QImage result(virt.size(), QImage::Format_RGB32); + result.fill(Qt::black); + QPainter painter(&result); + for (QScreen *s : QGuiApplication::screens()) { + QImage screenImg = s->grabWindow(0).toImage(); + QRect target = s->geometry().translated(-virt.topLeft()); + if (screenImg.size() != target.size()) + screenImg = screenImg.scaled(target.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + painter.drawImage(target.topLeft(), screenImg); + } + painter.end(); + return result; } diff --git a/src/screenshooter.h b/src/screenshooter.h index 3177ea7..315873b 100755 --- a/src/screenshooter.h +++ b/src/screenshooter.h @@ -17,9 +17,11 @@ #include #include #include +#include #include #include #include +#include class ScreenShooter {