How to add icon without a text into QListWidget?

  • I want to display icon in QListWidget without a text. I set the text to empty string and to size 1. When I select theicon I see an empty square below the icon on the text place.
    How can I get rid of this empty space?


  • @IL
    What happens if you only set the icon?

    QListWidgetItem *i;

  • @the_
    Same same!

  • Are you using stylsheets? Did you use listWidget->setViewMode(QListView::IconMode); ? Can you provide sample code of the problem?

  • @elveatles
    Yes I do using style-sheets, and I am not familiar with a way that style-sheets can resolve this issue.
    Icon mode set the text beneath the icon while list-mode set the text beside the icon.
    In both cases I have the same problem.
    I am using QtCreator, creating ListWidget and add items, my code is very basic.
    I can provide code from the ui_mainwindow.h if you point me to something specific.


  • Stylesheets can cause this kind of problem if you are using padding or margin. If you try to remove the stylesheet and create your QListWidgetItems the way "the_" suggested, it should work. If not there must be something I'm not thinking of.

  • @elveatles
    Hi and thanks,
    I have removed the style-sheet and create the item with icon only but it still save place for the text.
    I am a bit confused, I don't find any solution for that, something that looks trivial seems to be an issue.
    I will be very happy to know that someone has a solution for that.

    Thanks again,

  • Just another (maybe crazy and/or stupid) idea...
    Why not set a custom widget for the QListWidgetItem that looks as you expect it to look like instead of putting an icon to the QListWidgetItem ?

  • @the_
    What do you mean by saying custom widget?

  • @IL
    you can add a widget to an item. So just create a for example frame that contains your icon and add it to the list item.

    QListWidget *list = new QListWidget(this);
    QFrame *custom = new QFrame;
    //add whatever you need to the frame
    QListWidgetItem *i = new QListWidgetItem;

    I used this for a list where each item needed several buttons to get for example detailed information about the item or delete the item from the list with one click

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.