7

I have a QTableView showing rows of a database table. In this table I have a column called data type and I have icon images for each type. How can I add these icons in front of each data type?

Here's a part of my code as requested by justanothercoder.

QString msgQueryString = "select MESSAGE_ID, DATA_TYPE from SER_MESSAGES where MESSAGE_ID > 500 ";
serendibMsgTableModel->setQuery(msgQueryString, *database);
serendibMsgTableModel->setHeaderData(0, Qt::Horizontal, tr("Message ID"));
serendibMsgTableModel->setHeaderData(1, Qt::Horizontal, tr("Data Type"));

serendibMsgProxyModel->setSourceModel(serendibMsgTableModel);
serendibMsgView->setModel(serendibMsgProxyModel);

"serendibMsgTableModel" is a QSqlQueryModel and "serendibMsgProxyModel" is a customized QSortFilterProxyModel. "serendibMsgView" is the QTableView I need the icons to be displayed, in the Data Type column.

Hope this helps for your answer.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
kasper360
  • 367
  • 2
  • 4
  • 14

2 Answers2

9

I saw that you've already picked an answer but since you are learning Qt I'll add a few things.

Taking a look at the excellent Qt documentation I suggest you overwrite this in your model:

QVariant QSqlTableModel::data ( 
            const QModelIndex & index,
            int role = Qt::DisplayRole ) const   [virtual]

There are various roles (int role = Qt::DisplayRole):

enum Qt::ItemDataRole : Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

Qt::DecorationRole : The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or Qpixmap)

Thus, what you need to do is return a QIcon or QPixmap in the data() function for the DisplayRole.

Another approach which might be more appropriate is to make use of delegates: For example ColorListEditor

Derick Schoonbee
  • 2,971
  • 1
  • 23
  • 39
  • Thanks a lot for your answer Derick. In fact, I had a feeling I could use delegates for this task, and I've been trying out some examples as well. Thanks for your example as well. – kasper360 Apr 05 '11 at 03:57
  • 2
    Quoting: "Thus, what you need to do is return a QIcon or QPixmap in the data() function for the DisplayRole." ... did you mean `DisplayRole` or `DecorationRole`? – HiFile.app - best file manager Nov 12 '18 at 13:53
7

Set the DecorationRole of your items to the QPixmap you want and it should work.

edit:

I guess that the icon depends on the value in the data type column.

int rowCount = serendibMsgTableModel->rowCount();

for(int row = 0; row < rowCount; row++)
{
    QModelIndex index = serendibMsgTableModel->index(row, 1);
    QVariant value = serendibMsgTableModel->data(index);
    static QPixmap s_invalidIcon(PATH_TO_INVALID_ICON);
    static QPixmap s_type1Icon(PATH_TO_TYPE1_ICON);
    static QPixmap s_type2Icon(PATH_TO_TYPE2_ICON);

    QPixmap icon(s_invalidIcon);

    if(value.toString() == "type1")
    {
        icon = s_type1Icon;
    }
    else if(value.toString() == "type2")
    {
        icon = s_type2Icon;
    }
    serendibMsgTableModel->setData(index, icon, Qt::DecorationRole);
}

Something like this should work. Set the values before setModel.

I haven't tested it, but I think you should get the idea from this.

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
0xbaadf00d
  • 2,535
  • 2
  • 24
  • 46
  • Thanks justanothercoder. But I need a little more detailed description than that since I'm absolutely new to Qt. I'm learning Qt as I'm developing this application. – kasper360 Apr 04 '11 at 06:55
  • I'm a bit unaware of where to begin, you need to add a bit more description of what you have done. Adding some source code to the question might be good. – 0xbaadf00d Apr 04 '11 at 07:23
  • I have modified the original question and added a code snippet. – kasper360 Apr 04 '11 at 09:32
  • Thank u very much justanothercoder. I'll try this n let u know how it went. :) – kasper360 Apr 04 '11 at 10:28