My task is to transfer what is drawn in a user canvas to a QImage. In this case, the QML canvas must be created dynamically, since there can be many QImages.
I need to implement this code:
QList<QImage> imgs;
for (int i = 0; i < elements.length(); ++i) {
QImage img = paintImageElements(elements[i]);
imgs.append(img);
}
Trying to implement this:
QImage paintImageElements(const QString& elements) {
QQmlContext* context = QQmlEngine::contextForObject(this);
QQmlEngine *engine = context->engine();
QQmlApplicationEngine *appEngine = qobject_cast<QQmlApplicationEngine*>(engine);
QQmlComponent imageSceneComp(engine, QUrl(QStringLiteral("qrc:/ImagePainter/ImageScene.qml")));
QVariantMap initialProps;
initialProps["elements"] = elements;
QObject *imageSceneObj = imageSceneComp.createWithInitialProperties(initialProps, appEngine->rootContext());
auto grabResult = item->grabToImage();
return grabResult->image();
}
How do I implement the paintImageElements function correctly?
Because at the moment I get an error:
qrc:/ImagePainter/ImageScene.qml:7:1: QML ImageScene: grabToImage: item is not attached to a window
QQuickRenderControlclass, or at least that is one way. If you solve this yourself, please post a self-answer. Looking at Qt 6.9 examples (qquickrendercontrol to example search box), 3 are found, so maybe look at those.