This request is exactly what the Screenshot Example does.
On my Linux box if there is no delay between the hide() and the screenshot the KDE window manager may not have faded out the "hidden" window before the screen is grabbed. Turning off KDE desktop effects or introducing a long enough delay (~500 ms) avoids this. I would not be surprised if similar quirks are seen with over window managers. The screen shot example does not allow hiding the window if no delay is selected.
In-a-nutshell version:
#include <QtGui>
Q_OBJECT
public:
QTimer::singleShot(3000,
this,
SLOT(onButtonClicked
()));
// fake clicking a button after 3 secs }
public slots:
void onButtonClicked() {
hide();
QTimer::singleShot(500,
this,
SLOT(snapshot
()));
// long enough for window manager effects }
void snapshot() {
p.save("screen.png");
show();
QTimer::singleShot(3000,
qApp,
SLOT(quit
()));
// close the app in 3 secs }
};
int main(int argc, char **argv) {
Window w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
class Window: public QWidget {
Q_OBJECT
public:
explicit Window(QWidget *p = 0): QWidget(p) {
QTimer::singleShot(3000, this, SLOT(onButtonClicked())); // fake clicking a button after 3 secs
}
public slots:
void onButtonClicked() {
hide();
QTimer::singleShot(500, this, SLOT(snapshot())); // long enough for window manager effects
}
void snapshot() {
QPixmap p = QPixmap::grabWindow(QApplication::desktop()->winId());
p.save("screen.png");
show();
QTimer::singleShot(3000, qApp, SLOT(quit())); // close the app in 3 secs
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
Window w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks