1

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
1
  • 1
    I think you may need to use QQuickRenderControl class, 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. Commented Apr 23 at 17:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.