How to show icons without text in QListWidget?

I want to show only icons in my QListWidget. I set the text to an empty string. When I select the icon, I see the empty selected square in the text place. See the screenshot. How can I get rid of this empty space ?!

+6
source share
3 answers

use null instead

ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"),NULL));

+7
source

How to add an icon to my QListWidget? This should work fine (I am loading the icon from the resource file):

 ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"), "")); 

EDIT

In the screenshot, I see that your problem is that there is some space above the icon corresponding to the empty line. You can crack this behavior by setting the font size of the list widget element to very small.

 QListWidgetItem *newItem = new QListWidgetItem; QFont f; f.setPointSize(1); // It cannot be 0 newItem->setText(""); newItem->setIcon(QIcon(":/res/icon")); newItem->setFont(f); ui->listWidget->addItem(newItem); 

This will do the trick. However, you can also use the setItemWidget function and use your custom widget or use the QListView and delegate.

+4
source

My solution was to call setSizeHint () on an element with icon size. I added a small addition because the selection window was disabled without it.

 QListWidgetItem * pItem = new QListWidgetItem(icon, ""); pItem->setSizeHint(iconSize + QSize(4,4)); listWidget->addItem(pItem); 
+1
source

Source: https://habr.com/ru/post/900858/


All Articles