I have tried numerous ways to display a QIcon in a QTableWidget cell and I am not sure why it is not working. I have a button that when pressed adds a row to the table. Here is the code...
void MainWindow::pressed()
{
QTableWidgetItem *item = new QTableWidgetItem("Hello, world!");
QTableWidgetItem *icon_item = new QTableWidgetItem;
QIcon icon("/verified/path/to/icon.png");
icon_item->setIcon(icon);
int row = ui->tableFeed->rowCount();
ui->tableFeed->insertRow(row);
ui->tableFeed->setItem(row, 0, icon_item);
ui->tableFeed->setItem(row, 1, item);
}
And it just doesn't work. Nothing is displayed in that cell. Any ideas?
EDIT: the setItem call where I set it to icon
was a typo. The actual code sets it to the QTabeWidgetItem icon_item
. I have corrected it in the code above.
You have to set the icon to a QTableWidgetItem
and then load the icon item and not the icon itself.
QTableWidgetItem *item = new QTableWidgetItem("Hello, world!");
QTableWidgetItem *icon_item = new QTableWidgetItem;
QIcon icon("/verified/path/to/icon.png"); // It is better to load the icon from the
// resource file and not from a path
icon_item->setIcon(icon);
int row = ui->tableFeed->rowCount();
ui->tableFeed->insertRow(row);
ui->tableFeed->setItem(row, 0, icon_item);
ui->tableFeed->setItem(row, 1, item);
If you can see the string item and not the icon then something is wrong with the icon path.