How to take Screenshot of QML application without QQuickView

前端 未结 3 842
深忆病人
深忆病人 2021-02-15 11:55

I want to grab screen of my QML application. But my root QML object is ApplicationWindow, so I can\'t use QQuickView to show it. Instead I have to use

3条回答
  •  醉话见心
    2021-02-15 12:53

    You can cast ApplicationWindow(QML) to QQuickWindow(C++). Then, It will be easy to take screenshot.

    void ScreenShot(QQuickWindow *widget) {
        QPixmap pixmap = QPixmap::fromImage(widget->grabWindow());
        QFile f("your_name.png");
        f.open(QIODevice::WriteOnly);
        if(f.isOpen()) {
            pixmap.save(&f, "PNG");
        }
    

提交回复
热议问题