screenshot of a qt application from inside the application

泪湿孤枕 提交于 2019-12-06 04:24:30

问题


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


回答1:


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.




回答2:


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++;
    }
}



回答3:


QPixmap lets you do a window grab if you have the ID. My references are for PyQt but I'm sure you can make the adjustments:

How to get RGB values of QPixmap or QImage pixel - Qt, PyQt

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpixmap.html#grabWindow




回答4:


In modern QT 5+ it can be done like that:

void MainWindow::takeScreenshot(const QString screenshotFileName)
{
    if(isActiveWindow())
    {
        auto grabbedScreenshot = QWidget::grab();
        grabbedScreenshot.save(screenshotFileName);
    }
}


来源:https://stackoverflow.com/questions/9169329/screenshot-of-a-qt-application-from-inside-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!