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?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • See: http://doc.qt.io/qt-5/qquickimageprovider.html – derM - not here for BOT dreams Dec 11 '17 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" }` – TheWaterProgrammer Dec 11 '17 at 20:38
  • I have defined my own `QQuickImageProvider` derived class & overriding `requestImage` which is getting called as well. But I could not understand how is the `source: ` property in `Image` element set ? – TheWaterProgrammer Dec 11 '17 at 20:39
  • what does the image change? – eyllanesc Dec 11 '17 at 20:41
  • 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. – TheWaterProgrammer Dec 11 '17 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. – TheWaterProgrammer Dec 11 '17 at 20:52
  • 1
    looks like this `QQuickImageProvider` derived class way is nice but how to trigger/manually initiate a call to `requestImage`? – TheWaterProgrammer Dec 11 '17 at 20:53

1 Answers1

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
  • 1
    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 – TheWaterProgrammer Dec 11 '17 at 20:48
  • 1
    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. – derM - not here for BOT dreams Dec 11 '17 at 20:55
  • 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 ? – TheWaterProgrammer Dec 11 '17 at 21:00
  • is it somehow possible to trigger a call to `requestImage` after setting a `potentialId`? – TheWaterProgrammer Dec 11 '17 at 21:57
  • `id` is always received as blank in `requestImage` – TheWaterProgrammer Dec 11 '17 at 22:25
  • Whenever you change the string you assign to the `source`, the engine will look up for a new picture. If the protocol is `image` it will look in the registered image providers, for the one that is specified by the second part (`registeredNameOfImageProvider` resp. `myimg`). If specified it passes the thrid part of the string (`potentialId`) as value of `&id` to the function `requestImage`. If not specified it will pass an empty string instead. In my function, `id` will be ignored, but you are free to handle it and pass another image instead... – derM - not here for BOT dreams Dec 11 '17 at 22:50