3

I am have a QImage object in my Qt app on the C++ side of code.

Image {
   id: my_image
   source: ""
}

I have the QML Connections element in which I am getting a QImage sent

Connections {
    target: qimage_supplier
    onNewQImage: {
        recalled_media_image.source = new_qimage_supplied
    }
}

Question:
Is it possible to set a QImage object to the source of a Image QML item? Doing above way where new_qimage_supplied is actually a QImage sent from C++ side, it complains the following:

Error: Cannot assign QImage to QUrl

How can I set a QImage object to Image QML element?

7
  • See: doc.qt.io/qt-5/qquickimageprovider.html Commented Dec 11, 2017 at 20:27
  • what is image.png in the example where source is set to the imageprovider as image://MyImageProvider/Image.png . the full declaration is like this.Image { source: "image://MyImageProvider/Image.png" } Commented Dec 11, 2017 at 20:38
  • 1
    I have a void SetImage(QImage image) in my QQuickImageProvider derived class which I call from CPP side to set an image whenever available from some processing. Commented Dec 11, 2017 at 20:45
  • 1
    @eyllanesc its a new QIMage I get on C++ side every time with the same height & width but the content is totally different. all I want to do is set the new QImage object on to the Image QML item from C++ side whenever I get a new QImage . thats it. Commented Dec 11, 2017 at 20:52
  • 1
    looks like this QQuickImageProvider derived class way is nice but how to trigger/manually initiate a call to requestImage? Commented Dec 11, 2017 at 20:53

1 Answer 1

2

This is a quick and dirty example (not for copy and paste!)

// in main.cpp (imports are missing)

class MyImageProvider: public QQuickImageProvider
{
    QImage m_image;
public:
    MyImageProvider(QImage img)
        : QQuickImageProvider(QQuickImageProvider::Image)
        , m_image(img)
    {}

    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override {
        qDebug() << m_image;
        return m_image;
    }
};

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;   
    engine.addImageProvider("myimg", new MyImageProvider(QImage("C:/.../image.png")));
    engine.load(QUrl(QStringLiteral("main.qml")));

    return app.exec();
}

// in main.qml

ApplicationWindow {
    id: rootWin
    width: 800; height: 600; visible: true

    Image {
        source: "image://myimg/1"
    }
}

The source is:

image://registeredNameOfImageProvider/potentialId
Sign up to request clarification or add additional context in comments.

6 Comments

how can I trigger a call to requestImage? I have a method in MyImageProvider to set the new QIMage whenever available. If I could just somehow manually trigger the call to requestImage somehow then the problem is solved for me
The problem is, that afaik the engine caches the images. So you will need to change the source everytime. Change from source: "image://myimg/1" to source = "image://myimg/2" and so on, everytime the image changes.
where do I get the id that is 1 or 2. is this the first parameter in requestImage, const QString &id that I need to override ?
is it somehow possible to trigger a call to requestImage after setting a potentialId?
id is always received as blank in requestImage
|

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.