Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableView with icons in rows

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.

like image 974
kasper360 Avatar asked Apr 04 '11 06:04

kasper360


1 Answers

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

like image 129
Derick Schoonbee Avatar answered Oct 08 '22 21:10

Derick Schoonbee