I am trying to display an image from a QAbstractTableModel. I tried returning a QPixmap as the QVariant of data(), but it only produces empty cells, when I would expect every cell in the second column to have a 20×20 black square.
This is my code currently:
QVariant MySqlTableModel::data(const QModelIndex &idx, int role = Qt::DisplayRole) const
{
if (role == Qt::DisplayRole && idx.column() == 1) {
QPixmap pixmap(20,20);
QColor black(0,0,0);
pixmap.fill(black);
return pixmap;
}
return QSqlTableModel::data(idx, role);
}
Only
QVariants that can be converted to string can be returned for the roleQt::DisplayRolewith the standard delegate.You can show the image by returning it for the role
Qt::DecorationRoleOr write your own delegate to do the painting yourself. See QStyledItemDelegate documentation for more details.