How to get and save screenshot in Qt

👁 33 просмотров

We can to get screen of device or screen of running app.

Code for get screen of device and save in .png format:

    QPixmap p;
    QScreen *screen = QGuiApplication::primaryScreen();
    if (screen)
       p = screen->grabWindow(0);//(0, 0, 0, 100, 20);//WinId, X, Y, width, height
    QImage qImage = p.toImage();
    qImage.save("screen.png");

Code for get screen of running app and save in .png format:

    QPixmap p = this->grab();
    QImage qImage = p.toImage();
    qImage.save("screen.png");