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

Stephen Kelly steveire at gmail.com
Fri Feb 18 17:18:42 CET 2011


Girish Ramakrishnan wrote:

> 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");

What about 
{
  QPixmap p1("foo.png");
}
{
  QPixmap p2("foo.png");
}

?

Does the cache key get invalidated on dtor?

> 
> 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