In windows this is very simple:
auto pixmap = qApp->screens().at(0)->grabWindow(QDesktopWidget().winId());
But grabWindow isn't working on linux. I try something like:
QScreen *screen = QGuiApplication::primaryScreen();
auto pixmap = QPixmap::grabWindow(0);
but with no good result.
Should work if screen is valid. You can refer to shootScreen method at Qt Screenshot Example