Results 1 to 4 of 4

Thread: Hide a QIcon in a QListWidget

  1. #1
    Join Date
    Sep 2010
    Location
    Washington, PA
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Hide a QIcon in a QListWidget

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide a QIcon in a QListWidget

    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

    Qt Code:
    1. QPixmap px(16,16);
    2. px.fill(Qt::transparent);
    3. item->setIcon(QIcon(px));
    To copy to clipboard, switch view to plain text mode 

    or something smarter (i.e. to make use of QPixmapCache).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Washington, PA
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Hide a QIcon in a QListWidget

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hide a QIcon in a QListWidget

    Quote Originally Posted by KineticArc View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Add pictures svg in QIcon
    By giorgik in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2010, 09:00
  2. Qicon
    By shihao in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2010, 13:23
  3. Change QIcon
    By Diph in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2009, 07:29
  4. How to get the QIcon fileneme in use.
    By gt.beta2 in forum Newbie
    Replies: 1
    Last Post: 25th February 2009, 15:30
  5. Replies: 2
    Last Post: 31st May 2006, 21:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.