Important: Please read the Qt Code of Conduct -

QT_AUTO_SCREEN_SCALE_FACTOR and grabWindow issue

  • Hey everyone,

    I have an issue with QScreen::grabWindow function and it all started after introducing qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1") into the project.

    I've separared the issue in this small piece of code:

    #include <QApplication>
    #include <QScreen>
    #include <QPainter>
    #include <QDesktopWidget>
    int main(int argc, char *argv[])
        qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
        QApplication a(argc, argv);
        QList<QPixmap> sourcePixmaps;
        int totalWidth = 0, totalHeight = 0;
        foreach(QScreen* screen, QApplication::screens())
            QRect screenGeometry = screen->geometry();
            QPixmap screenPixmap = screen->grabWindow(QApplication::desktop()->winId(), screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height());
            totalWidth += screenPixmap.width();
            totalHeight = std::max(totalHeight, screenPixmap.height());
        QPixmap finalPixmap(totalWidth, totalHeight);
        QPainter painter(&finalPixmap);
        int currentPos = 0;
        foreach(QPixmap sourcePixmap, sourcePixmaps)
            painter.drawPixmap(QPoint(currentPos, 0), sourcePixmap);
            currentPos += sourcePixmap.width();

    This code should take the screenshots of all monitors and combine them into one image.

    Environment: QtCreator, Qt 5.9.1 mingw
    Monitor setup:
    Monitor setup
    Running result (screenshot.png):

    I can't get it to look good, and it has to look good on various
    positions of monitors, with various resolutions and various DPIs.

    When I debug this code, there is a couple of things I don't understand:

    1. Why is screen->geometry() returning values that are just partially scalled by scale factor? Example for secondary screen (x = 1920, y = 0, w = 920, h = 540)
    2. What coordinates does grabWindow() expect actually?
    3. Why does grabWindow() doesn't return the pixmap of size that same as the
      rectangle size i specified in grabWindow?

    Without setting QT_AUTO_SCREEN_SCALE_FACTOR everything works fine, keep that in mind.

    Please help, if someone has some experience with this.

    Kind regards,

  • Lifetime Qt Champion

    the screenshot.png is not visible.
    (forum bug)

Log in to reply