title: Take screenshot with Qt C++
body: <p>I'm trying to take a screenshot with my application. But I get the following error message :</p>
  
  <pre><code>QPixmap: Must construct a QGuiApplication before a QPixmap
  </code></pre>
  
  <p>My program runs in the background and I do not want a GUI. How would I create a "QGuiApplication" and then hide the window permanently ? Or is there another way to take a screenshot without having to create a GUI ?</p>
  
  <p>I was using the following code to take the screenshot :</p>
  
  <pre><code>QScreen *screen;
  QPixmap qpx_pixmap;
  
  screen = QGuiApplication::primaryScreen();
  qpx_pixmap = screen-&gt;grabWindow(0);
  screenshotTarget = dir.path() + "/" + QDateTime::currentDateTime().toString("dddd hh:mm:ss");
  qpx_pixmap.save(screenshotTarget);
  </code></pre>