Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

screenshot of a qt application from inside the application

I am trying to capture a screenshot of my application within the application. Its a Qt-based application. Is anyone aware of how to do this? Any suggestions are very welcome.

CV

like image 818
Chenna V Avatar asked Feb 22 '23 14:02

Chenna V


2 Answers

You can tell any QWidget (including your QMainWindow) to render itself off-screen: http://developer.qt.nokia.com/doc/qt-4.8/qwidget.html#render

Technically this is not a screenshot as it renders the widget explicitely for this purpose instead of capturing what is seen on-screen. For almost any purpose it doesn't matter.

If you have a GL widget, you can/must instead use grabFramebuffer() which has the advantage of capturing what is seen on the screen.

like image 192
ypnos Avatar answered Mar 04 '23 11:03

ypnos


With this example you could get all your widget screen. You could attach this method to any key press or signal, as you prefer, to get successive screenshot.

MyClass::screenshot()
{
    QWidget *w = QApplication::activeWindow();
    if(w) {
        static int count = 0;
        QPixmap p = QPixmap::grabWidget(w);
        p.save(QString("/your/path/screenshot%1.png").arg(count));
        count++;
    }
}
like image 26
kikeenrique Avatar answered Mar 04 '23 11:03

kikeenrique