Taking screenshot of a specific/focused window
-
but it did not work
Because it's totally bonkers.
QPixmap::grabWindowexpects a window handle (WinId).QApplication::desktop()returns a widget andhasFocus()returns abool. You're casting aboolto 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::grabWindowis 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); } -
but it did not work
Because it's totally bonkers.
QPixmap::grabWindowexpects a window handle (WinId).QApplication::desktop()returns a widget andhasFocus()returns abool. You're casting aboolto 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::grabWindowis 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); } -
Nothing in Qt for that. It's very platform specific so you'll need to go for the native APIs.
-
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 -
@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.