[Qt-interest] Is Qt smart anough not to load the same icon resource multiple times?

Girish Ramakrishnan girish at forwardbias.in
Thu Feb 17 18:20:13 CET 2011


Hi Daniel,

On Thu, Feb 17, 2011 at 3:36 PM, Daniel Price <daniel.price at fxhome.com> wrote:
> If I create multiple instances of a custom widget that loads an icon from a
> resource like this:
>
>
>
> m_icon(QPixmap(“:/MyIcons/MyIcon.png”))
>
>
>
> is Qt smart enough to load the icon only once and implicitly share the
> resource? Or it is loading the same icon multiple times and wasting memory?
>
>

Yes. In addition to implicit sharing, QImage/QPixmap have a mechanism
to share the data if they are from the same source. See
QPixmap/QImage::cacheKey.

This means that even with implicit sharing is not in use,
QPixmap p1("foo.png");
QPixmap p2("foo.png");

will load foo.png only once. Both p1 and p2 have same cacheKey and
will refer to the same data.

Girish



More information about the Qt-interest-old mailing list