何在没有QQuickView的情况下拍摄QML应用程序的屏幕截图

何在没有QQuickView的情况下拍摄QML应用程序的屏幕截图

本文介绍了如何在没有QQuickView的情况下拍摄QML应用程序的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我的QML应用程序的屏幕.但是我的根QML对象是ApplicationWindow,所以我不能使用QQuickView来显示它.相反,我必须使用QQmlApplicationEngine.我知道如何使用QQuickView(QQuickWindow::grabWindow)截屏,但是不知道如何使用QQmlApplicationEngine截屏.

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 QQmlApplicationEngine.I know how to take screenshot with QQuickView (QQuickWindow::grabWindow), but don't know how to take it with QQmlApplicationEngine.

我的main文件:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    ApplicationData appData;
    QQmlApplicationEngine engine;
    engine.load(QUrl("qrc:///main.qml"));
    return app.exec();
}

main.qml:

import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Window 2.1


ApplicationWindow {
    id: mainWindow
    width: appData.screenWidth
    height: appData.screenHeight
    visible: true

    Rectangle {
        id: mainWindowBackground
        color: "red"
        anchors.fill: parent
    }

    //...
}

推荐答案

您可以使用rootObjects()并将其结果转换为QQuickWindow:

You can use rootObjects() and cast its result to QQuickWindow:

foreach(QObject* obj, engine.rootObjects()) {
  QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
  if (window) {
    QImage image = window->grabWindow();
    qDebug() << image;
  }
}

这篇关于如何在没有QQuickView的情况下拍摄QML应用程序的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:57