PDA

View Full Version : Get Desktop Screenshot Without Application Window Being Shown



personk
9th August 2013, 03:55
I know how to get a screenshot of the desktop (
QPixmap::grabWindow(QApplication::desktop()->winId())). But, I want to exclude the window of the application that takes the screenshot.

I tried to hide the window right before taking the screenshot, and show it right after. But this didn't work.

Thanks.

ChrisW67
9th August 2013, 04:55
What about it "didn't work"?

Rajesh.Rathod
9th August 2013, 07:15
You can try this.
QApplication::desktop()->raise();
QPixmap pixmap(QPixmap::grabWindow(QApplication::desktop()->winId()));

ChrisW67
9th August 2013, 08:19
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>
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"

personk
10th August 2013, 00:24
First, thanks all.

But It seems that I chose the wrong approach to achieve my goal.

I wanted to make an application that magnifies the area covered by the application window. My approach was to get a screenshot at the specified rectangle, then scale the image to look magnified. This effect is supposed to work as I drag the window. So, the idea of hiding/showing the application window will not be comfortable.

Is there a method to get the desktop screenshot without the application window, without hiding it?

I feel like I have to get my hands in some window manager/display server work. I hope I am wrong.

Note: I looked at magnifier applications like KMag. But, it doesn't achieve the goal I stated above.

wysota
10th August 2013, 10:52
I think that to do what you want, you would have to tap into the window compositor itself (e.g. implement your app as a KWin effect plugin) as what you require is obtaining the desktop composition state before your window is rendered and then rendering and compositing the result into the final desktop display.