Solved Taking screenshot of a specific/focused window
-
i want get the screen shot of a widget (like listview in my program) and focused window
i tried thisui->label->setPixmap(QPixmap::grabWindow(QApplication::desktop()->hasFocus()));
but it did not worked.it garbed whole screen.
-
but it did not work
Because it's totally bonkers.
QPixmap::grabWindow
expects a window handle (WinId).QApplication::desktop()
returns a widget andhasFocus()
returns abool
. You're casting abool
to a window handle which makes absolutely no sense. It probably casts to a null handle, which on most platforms means "desktop" so that's what you're getting.Apart from that
QPixmap::grabWindow
is obsolete and you should not use it in new code.Here's how to do it correctly:
auto active_window = qApp->activeWindow(); if (active_window) //could be null if your app doesn't have focus { QPixmap pixmap(active_window->size()); active_window->render(&pixmap); ui->label->setPixmap(pixmap); }
-
@Chris-Kawa
thanks.it works.
now it takes screenshot of it's own .
what about get other focused app's screenshot? -
Nothing in Qt for that. It's very platform specific so you'll need to go for the native APIs.
-
@Chris-Kawa
i am in linux
i found this .but could not figure it out ,how to get other focused app's screen-shot.
here -
@saber
Hi
That is for windows.
If you dont want to get into X 11 programming and the millions windows managers on linux, you
could just include a cmdline screenshot utility and call it with QProcess
https://askubuntu.com/questions/194427/what-is-the-terminal-command-to-take-a-screenshot
shutter is pretty neat.If you dont want that, you can look at wmctrl source as it enum windows on various linux distros.
http://tripie.sweb.cz/utils/wmctrl/#help -
@mrjj
thanks for the suggestion.
i won't do this.
i don't want to add a dependencies for that option in my app. -
@saber
ok. fair enough.
User can always take shots themselfs.