Qt: render QML window to buffer

1k views Asked by At

My code looks like this:

QApplication app(argc, argv);
QQmlApplicationEngine app_engine;

app_engine.load("qml/main.qml");
return app.exec();

Could somebody please help me how to make Qt render everything to the buffer I provide? OpenGL must be avoided. I could make this work with QWebPage, but this appears to be much more difficult to me...

1

There are 1 answers

1
tselmeci On

I've found the way to get the QML output as QImage, but it works only if the QML window has focus. Incomplete code snippet follows:

QApplication app(argc, argv);
QQmalApplicationEngine *appEngine = new QQmlApplicationEngine(this);
appEngine->load(script_path);
...
app.exec();

While app is running, you can grab window contents like this:

QQuickWindow *win = qobject_cast<QQuickWindow *>(appEngine->rootObjects().first());
QImage grabbed = win->grabWindow();

It has several drawbacks (i.e. cursor disappears when the input focus is lost, grabWindow() is very slow etc.).

Furthermore, it's also possible to redirect QML page rendering to a custom FBO, this provides a much faster solution but also suffers from some issues.