How to show icons without text in QListWidget?
Asked Answered
S

4

8

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?!

Spotless answered 5/11, 2011 at 23:7 Comment(0)
P
10

use NULL instead

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

Postdoctoral answered 2/7, 2014 at 22:59 Comment(0)
R
5

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.

Reorganization answered 5/11, 2011 at 23:21 Comment(3)
An empty space is shown instead the text. This space is selected when you select the icon. I want to get rid of it. Corrected my question.Spotless
Setting small font doesn't do the trick. A small rectangle is still visible after selection. setItemWidget works strangely, it still displays an empty rectangle. I have added QLabel with a pixmap as widget. The last hope is the delegate. Can you post a code snippet, how to achieve my goal?Spotless
Check this question. You have to reimplement the paint function and the size hint. In your case you will only add an icon the way you want it. Then you just call the setItemDelegate.Reorganization
A
2

My solution was to call setSizeHint() on the item with the size of the icon. I added a little padding because the selection box was cut off without it.

QListWidgetItem * pItem = new QListWidgetItem(icon, "");
pItem->setSizeHint(iconSize + QSize(4,4));
listWidget->addItem(pItem);
Atiptoe answered 12/6, 2014 at 18:25 Comment(0)
D
1

An alternative solution where you do want to store text (as an identifier) but not show it, is not to set ANY text for the QListWidgetItems in the creator but instead store the text details in the data part.

Specifically you want to use this QListWidgetItem(QListWidget *parent = nullptr, int type = Type) constructor that can be used without any arguments. Then you can assign an icon, but no text afterwards, before inserting it into the QListWidget.

If you are putting the text in afterwards anyhow you'd be using QListWidgetItem::setText(const QString &text) and you just need to change that to QListWidgetItem::setData(int role, const QVariant &value) which in practice would be QListWidgetItem::setData(Qt::UserRole, const QString &text) and rely on a QString being convertible to a QVariant. Unfortunately there are some shortcomings going the other way:

  • To retrieve the value you need to explicitly convert the QVariant back to a QString - by using QListWidgetItem::data(Qt::UserRole)->toString()
  • To search for a particular match Qt does not provide a find means to identify the index(es) of the members of a QListWidget that match a particular role though it does for the original text - unlike, say a QComboBox which has findXXXX methods for both.

The project I code for had exactly this issue (we had unwanted text at the bottom which was set to a font size of 1 but it still showed up) which I eventually fixed like this and it works:

Screenshot from Mudlet application showing a QListWidget filled with QListWidgetItems but with no text underneath them

Direful answered 8/7, 2020 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.