19 #include "screengrabber.h"
23 #include <QStandardPaths>
24 #include <QtGui/QImage>
25 #include <QtGui/QGuiApplication>
26 #include <QtQuick/QQuickWindow>
27 #include <QtConcurrent/QtConcurrentRun>
31 void saveScreenshot(QImage screenshot, QString filename, QString format,
int quality)
33 if (!screenshot.save(filename, format.toLatin1().data(), quality))
34 qWarning() <<
"ScreenShotter: failed to save snapshot!";
37 ScreenGrabber::ScreenGrabber(QObject *parent)
41 QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
42 screenshotsDir.mkdir(
"Screenshots");
43 screenshotsDir.cd(
"Screenshots");
44 if (screenshotsDir.exists())
46 fileNamePrefix = screenshotsDir.absolutePath();
47 fileNamePrefix.append(
"/screenshot");
51 qWarning() <<
"ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
55 void ScreenGrabber::captureAndSave()
57 if (fileNamePrefix.isEmpty())
59 qWarning() <<
"ScreenShotter: no directory to save screenshot";
63 const QWindowList windows = QGuiApplication::topLevelWindows();
66 qWarning() <<
"ScreenShotter: no top level windows found!";
70 QQuickWindow *main_window = qobject_cast<QQuickWindow *>(windows[0]);
73 qWarning() <<
"ScreenShotter: can only take screenshots of QQuickWindows";
77 QImage screenshot = main_window->grabWindow();
78 QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
81 QString ScreenGrabber::makeFileName()
83 QString fileName(fileNamePrefix);
84 fileName.append(QDateTime::currentDateTime().toString(
"yyyyMMdd_hhmmsszzz"));
86 fileName.append(getFormat());
90 QString ScreenGrabber::getFormat()