Screenshot capture of dual monitor in Qt 5
<p>In the context of a Qt application, I'm using the following code snippet for taking a screenshot of full desktop:</p>

<pre><code>QDesktopWidget* dw = QApplication::desktop();
QPixmap pixmap = QPixmap::grabWindow(dw-&gt;winId(), 0, 0,
                                     dw-&gt;width(), dw-&gt;height());
pixmap.save(name, "JPG", screenshot_quality);
</code></pre>

<p>This approach works pretty well in Linux and Windows and with dual monitor, independently of screen's resolutions; that is, it works still if the two monitors are working with different resolutions. However, with Qt 5 I get the following run-time warning:</p>

<pre><code>static QPixmap QPixmap::grabWindow(WId, int, int, int, int) is deprecated, use QScreen::grabWindow() instead. Defaulting to primary screen.
</code></pre>

<p>So I reviewed the Qt 5 doc and I wrote this:</p>

<pre><code>QScreen * screen = QGuiApplication::primaryScreen();
QPixmap pixmap = screen-&gt;grabWindow(0);
pixmap.save(name, "JPG", screenshot_quality);
</code></pre>

<p>But this approach does not capture the second screen.</p>

<p>So I searched a little more and, according to this thread, <a href="https://stackoverflow.com/questions/24213942/taking-screenshot-of-full-desktop-with-qt5">Taking Screenshot of Full Desktop with Qt5</a>, I designed the screenshot capture as follows:</p>

<pre><code>QScreen * screen = QGuiApplication::primaryScreen();
QRect g = screen-&gt;geometry();
QPixmap pixmap = screen-&gt;grabWindow(0, g.x(), g.y(), g.width(), g.height());
pixmap.save(name, "JPG", screenshot_quality);
</code></pre>

<p>Unfortunately, this does not work too.</p>

<p>What catches my attention is that the method with Qt 4 works well. Since I imagine there must be some way to make it in Qt 5.</p>

<p>So, my question is how can be done with Qt 5?</p>

<p>EDIT: This is the way as I solved:</p>

<pre><code>QPixmap grabScreens()
{
  QList&lt;QScreen*&gt; screens = QGuiApplication::screens();
  QList&lt;QPixmap&gt; scrs;
  int w = 0, h = 0, p = 0;

  foreach (auto scr, screens)
    {
      QRect g = scr-&gt;geometry();
      QPixmap pix = scr-&gt;grabWindow(0, g.x(), g.y(), g.width(), g.height());
      w += pix.width();
      h = max(h, pix.height());
      scrs.append(pix);
    }

  QPixmap final(w, h);
  QPainter painter(&amp;final);
  final.fill(Qt::black);
  foreach (auto scr, scrs)
    {
      painter.drawPixmap(QPoint(p, 0), scr);
      p += scr.width();
    }

  return final;
}
</code></pre>

<p>Thanks to @ddriver!</p>