PDA

View Full Version : Hide a QIcon in a QListWidget



KineticArc
14th October 2010, 15:18
OK, I'm sure I'm missing something here, but how do I set items in a QListWidget to have an icon that can be hidden? For instance, I want to have enough space for the items in the QListWidget to have a 16x16 icon. When the program starts, the icon will be hidden until the user selects an item in the list, the resulting form displayed (in an attached QScrollArea), and the form modified with user input. Once this happens, I want to emit a signal back to the main window which would then display the hidden "save file" icon.

This action is similar to how kate and kdevelop interact with edited files in the document view.

I've looked into trying to modify the QIcon directly, but there isn't a reference given back from QListWidget::icon(). I don't want to have to create an icon with all alpha blending (for transparency) with a size of 16x16 and flip-flop between icons. There has to be a way for me to sub-class QListWidgetItem or QListWidget for me to be able to dynamically show/hide the icon. All while not having the items in the list shift left to right when the icon is shown and hidden.

Anyone have any insight for me to be able to show/hide icons in a QListWidget while maintaining a 16x16 space between the left edge of the widget, and the start of the row's text?

Thanks!

wysota
14th October 2010, 15:26
You can provide your own delegate that will leave the space for the icon regardless of whether the icon is assigned or not. Although I'd probably go for an empty pixmap :)


QPixmap px(16,16);
px.fill(Qt::transparent);
item->setIcon(QIcon(px));

or something smarter (i.e. to make use of QPixmapCache).

KineticArc
14th October 2010, 20:48
This will work. Seems to me that flip-flopping the icons back and forth would create some overhead. Especially when the actual .png file is copied into memory. The .png file would then be deleted when I set the transparent icon. Whenever a change is made, and I reset the .png icon, the file is again copied into memory and reset in the item. Seems like it would be a little nicer if the icon itself had a hide method, or the pixmap returned as a reference. I'm sure this could cause problems if someone didn't know what they were doing with the referenced icon, but still...

Thanks for the help! I did this same thing, just didn't fill the pixmap with Qt::transparent.

wysota
14th October 2010, 21:11
This will work. Seems to me that flip-flopping the icons back and forth would create some overhead. Especially when the actual .png file is copied into memory. The .png file would then be deleted when I set the transparent icon. Whenever a change is made, and I reset the .png icon, the file is again copied into memory and reset in the item.
That's why I said to use QPixmapCache.


Seems like it would be a little nicer if the icon itself had a hide method, or the pixmap returned as a reference. I'm sure this could cause problems if someone didn't know what they were doing with the referenced icon, but still...
Implement a custom delegate that will simply not draw the icon if you don't want it to or do things the proper way - using the model approach.