[Interest] QIcon vs QQuickIcon

Benjamin TERRIER b.terrier at gmail.com
Tue May 31 10:45:40 CEST 2022


On Sun, 29 May 2022 at 16:31, ivan tkachenko <me at ratijas.tk> wrote:

> Hi,
>
> I have a problem which sounds like it should be something trivial.
>
> I got a QIcon icon, aquired from a model by a delegate. How do i use it in
> controls with all these icon.name [string], icon.source [url] groupped
> properties or even with just an Image type?
>
> All sane attempts failed. I'm left with this a decade-old StackOverflow
> answer
> to use custom Image Provider. Sounds like too much work for something that
> should be just here, out of the box?
>

A QIcon is a collection of images.
A QQuickIcon is just a struct containing a URL plus some options (width,
height, etc.).

Internally to render the icon, the QQuickIcon's URL is just forwarded to a
QQuickIconImage which will attempt to retrieve the image from the URL.
So there is no easy way to feed a QIcon to a QQuickIcon as you must use a
URL at some point.

The standard solution is to use a QQuickImageProvider.
In C++ you would implement your own QQuickImageProvider which will contain
the QIcons.
In QML then you would use a url like "image://myprovider/icon_id".
Then the QML engine will call your QQuickImageProvider to retrieve the icon
image.

An alternative solution might be to use data urls, but I have never tried
it.

BR

Benjamin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220531/4698a689/attachment.htm>


More information about the Interest mailing list