Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show icons without text in QListWidget?

I want to show only icons in my QListWidget. I set text to empty string. When I select an icon I see an empty selected square on the text place. See the screenshot:

Screenshot

How can I get rid of this empty space?!

like image 268
Dmitriy Avatar asked Nov 05 '11 23:11

Dmitriy


2 Answers

use NULL instead

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

like image 104
compiler Avatar answered Nov 04 '22 00:11

compiler


How do you add an icon in your QListWidget? This should work fine (I am loading the icon from the resource file) :

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

EDIT

From the screenshot I see that your problem is that there is some white space below the icon corresponding to the empty string. You could hack this behavior by setting a very small size to the font of the list widget item.

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 could also use the setItemWidget function and use your custom designed widget, or use a QListView and a delegate.

like image 4
pnezis Avatar answered Nov 04 '22 02:11

pnezis