-1

I have a QAbstractListModel with custom objects as items. Each object has a QImage that is loaded from a database. I use ListView in QML to visualize it but I do not see any mean to represent QImage in the delegate. Image primitive seems to accept only URLs.

Is the only way for me to show QImages is to create a QQuickImageProvider with some custom system of a URL per element (looks like a total overkill)?

Massimo Callegari
  • 2,099
  • 1
  • 26
  • 39
user14416
  • 2,922
  • 5
  • 40
  • 67

2 Answers2

2

I think QQuickImageProvider is the proper way.

Also, I think you can use the word 'overkill' if you know exactly how the Qt internals work. Otherwise it's just guessing.

AFAIK there is a complex caching system of images (and other data) underneath, so once an image pixmap is loaded (and doesn't change) data retrieval is immediate. So no overkill at all, since in any case at some point you need to load those QImage, but just once.

I believe a QQuickImageProvider provides pointers to the cached data, and not the whole rasterized data every time. Moreover blitting operations are nowadays performed with hardware accelerations, so it's a single operation taking a fraction of millisecond.

In other words you end up having:

  • give me image with url "image://xyz"
  • Qt looks up in the cache and returns the data pointer or performs a full load of the image if not found
  • the QML renderer passes the data array to OpenGL
  • one single blit operation (microseconds) and you have it on screen
Massimo Callegari
  • 2,099
  • 1
  • 26
  • 39
  • I do not see it as a proper way in my case, as I said I see it as an overkill. It is really sad that there is not easy way to visualize QImage. – user14416 Apr 02 '17 at 13:06
  • I have updated my answer. If the overkill is implementing a QQuickImageProvider then it's another story. But we are not lazy, are we ? :) – Massimo Callegari Apr 02 '17 at 13:57
  • )) I mean I just do not need QQuickImageProvider, since I have already loaded all the images necessary for me. But I understand that there is no easy way to do that. Thank you. – user14416 Apr 02 '17 at 14:24
0

A QML ShaderEffect will bind a QImage to a GLSL Sampler2D. See the list of "how properties are mapped to GLSL uniform variables" in the ShaderEffect docs. Needs a few lines of GLSL writing in the ShaderEffect to pass pixels through, (will update this post with an example sometime, when I have access to my code), but the result is a fast, QSG-friendly all-QML element which can render QImages.

timday
  • 24,582
  • 12
  • 83
  • 135