PDA

View Full Version : Best way to manage application icons



SiLiZiUMM
23rd April 2008, 14:28
Hello,

I was thinking about how should I manage the various icons that will be displayed in my application. I'll use a simple example to illustrate my problem. Let's say I have a custom button that displays an image next to the text. The button inherits from QPushButton, and contains a private QIcon member that loads the icon from the resource file.

Obviously, if I create 100 buttons, the image will be loaded 100 times in the memory. I searched a bit and found the QPixmapCache. Well, it should be possible to use this class to load the image instead, so the image would be shared among all button instances, which is better.

But what about if I want to display various images size? Well, I could create a QIcon and load the various images (different sizes) in it. But then, i cannot use QPixmapCache anymore... And also, if I would reuse that QIcon somewhere else, I would have to duplicate the code that loads the various pixmap sizes in the QIcon object, which doesn't feels right.

So far, I am planning to use a wrapper class (like a singleton) that uses a QCache to cache the QIcons, and a QPixmapCache to cache the various images. When asking for a specific QIcon, the class would look into the QCache for an existing instance. If it doesn't exist, it would load the icons into the QPixmapCache and return the QIcon.

But then, that class would have to know how to create *all* the icons, which seems to ask for a maintenance nightmare... :eek: Any ideas?