8

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.

linsek
  • 3,334
  • 9
  • 42
  • 55
  • Thanks for your sample code, it did work for me! Mine is like below, hope that is helpful. And i guess your problem is the image path. sIconPath = ":/images/NetworkStatusUnkown.png"; // image from resources QTableWidgetItem *item = new QTableWidgetItem(ss); // build up icon from status QIcon icon2(sIconPath); item->setIcon(icon2); ui->tableWidgetSearchResult->setItem( iRow, 0, item ); – JohnnyLinTW Mar 11 '15 at 04:54

4 Answers4

5

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.

pnezis
  • 12,023
  • 2
  • 38
  • 38
  • I can see the string item and not the icon, but I verified the path through the terminal and have double and triple checked this. Are there any other possible issues (icon size, etc)? – linsek Nov 10 '11 at 15:14
  • Can you paste the line where you construct your icon (with the path you are using)? – pnezis Nov 10 '11 at 15:20
  • `QIcon icon("/home/njozwiak/Pictures/icon.png");` – linsek Nov 10 '11 at 16:56
  • Try the following: Copy your icon in the folder where your source is, and create a new resource file. In the resource file create a new prefix eg icons and under it load your icon. Give this icons an alias eg icon. Now use this to load the icon `QIcon icon(":/icons/icon")`. For more details check this link http://doc.qt.nokia.com/4.7-snapshot/designer-resources.html. – pnezis Nov 10 '11 at 18:07
2

You'll have to first create a resource with prefix icons for these to work:

item = QtGui.QTableWidgetItem()
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/FSTable.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon2)

Pardon that the above is a PyQt code, but you can see the icon must be set up using addPixmap

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
swdev
  • 4,997
  • 8
  • 64
  • 106
2

When I want to add an icon to a cell, I usually do it in the model.

In the data method you can put them under the decoration role.

else if( role == Qt::DecorationRole )
{
    switch( index.column() )
    {
        case MOVIE:
        {
            if( valueAt( index ).toString() == "Godfather" ) return QIcon( ":/16x16/godfather.png" );
        }
    }
}

Hope this helps.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Wes
  • 4,781
  • 7
  • 44
  • 53
2

As webclectic pointed out, you probably want to set the item to icon_item:

ui->tableFeed->setItem(row, 0, icon_item);

...if it indeed compiled that way then I'd guess it's constructing an implicit QTableWidgetItem using one of its available constructors.

I'm not sure what happens if the icon can't be constructed from the specified png, but you might also check to make sure that the icon was indeed loaded correctly and can be displayed correctly. For example, what does icon.isNull() return? What happens if you put it in a label?

Another option is to load the icon from a pixmap so you can verify that it is indeed loaded properly:

QPixmap p;
if (!p.load(yourFilename) || p.isNull()) { qDebug() << "Error!"; }
QIcon icon = QIcon(p);
// and if wanted: label->setPixmap(p)
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
  • I added the line `if (icon.isNull()) { qDebug() << "Error: null icon"; }` to my source and no message was displayed. So I modified the path to a file that does not exist and I still didn't receive a message. So it looks like whether the file exists or not, `icon` is not null – linsek Nov 10 '11 at 17:03
  • How would I go about putting it in a label? – linsek Nov 10 '11 at 17:03
  • `label.setPixmap(icon.pixmap(width,height))`. I've also edited my post to include a second way to load the image. – Kaleb Pederson Nov 10 '11 at 17:48