Neko
Neko

Reputation: 3730

Using images in QListWidget, is this possible?

I am using QT to create a chat messenger client. To display the list of online users, I'm using a QListWidget, as created like this:

listWidget = new QListWidget(horizontalLayoutWidget);
listWidget->setObjectName("userList");
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(listWidget->sizePolicy().hasHeightForWidth());
listWidget->setSizePolicy(sizePolicy1);
listWidget->setMinimumSize(QSize(30, 0));
listWidget->setMaximumSize(QSize(150, 16777215));
listWidget->setBaseSize(QSize(100, 0));
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);

Users are shown by constantly refreshing the list, like this: (Note: There are different channels, with different userlists, so refreshing it is the most efficient thing to do, as far as I know.)

void FMessenger::refreshUserlist()
{
    if (currentPanel == 0)
        return;
    listWidget = this->findChild<QListWidget *>(QString("userList"));
    listWidget->clear();
    QList<FCharacter*> charList = currentPanel->charList();
    QListWidgetItem* charitem = 0;
    FCharacter* character;
    foreach(character, charList)
    {
        charitem = new QListWidgetItem(character->name());
        // charitem->setIcon(QIcon(":/Images/status.png"));
        listWidget->addItem(charitem);
    }
}

This has always worked perfectly. The line that I commented out is the one I have problems with: my current goal is to be able to display a user's online status with an image, which represents whether they are busy, away, available, etc. Using setIcon() does absolutely nothing though, apparently; the items still show up as they used to, without icons.

I'm aware that this is probably not the way this function needs to be used, but I have found little documentation about it online, and absolutely no useful examples of implementations. My question is, can anybody help me with fixing this problem?

Upvotes: 2

Views: 2215

Answers (1)

ypnos
ypnos

Reputation: 52317

This is how you may conduct your debugging:

  • Try the constructor that has both icon and text as arguments.
  • Try to use that icon in another context to ensure it is displayable (construct a QIcon with same argument and use it elsewhere, e.g. QLabel!).
  • Use icon() from the QListWidgetItem to receive back the icon and then look at that QIcon.
  • Create a new QListWidget, change nothing, and ordinarily add some stock items in your MainWidget's constructor. See if the icons show up there.

Upvotes: 3

Related Questions