Skip to content

Instantly share code, notes, and snippets.

@fecub
Last active August 29, 2015 14:07
Show Gist options
  • Save fecub/75e3660bfa12725ad6a6 to your computer and use it in GitHub Desktop.
Save fecub/75e3660bfa12725ad6a6 to your computer and use it in GitHub Desktop.
How to Qt screenshot
#include <QPixmap>
#include <QDir>
#include <QFileDialog>
#include <QString>
#include <QScreen>
QPixmap originalPixmap;
originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices.
//! [5]
QScreen *screen = QGuiApplication::primaryScreen();
if (screen)
originalPixmap = screen->grabWindow(0);
QString format = "png";
QString initialPath = QDir::currentPath() + QObject::tr("/untitled.") + format;
// QString fileName = QFileDialog::getSaveFileName(0,QObject::tr("Save As"),
// initialPath,
// QObject::tr("%1 Files (*.%2);;All Files (*)")
// .arg(format.toUpper())
// .arg(format));
QString fileName = "C:\\path\\to\\file\\testscreenshot.png";
if (!fileName.isEmpty())
originalPixmap.save(fileName, format.toLatin1().constData());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment