I'm curious about how I can display an image from my database in a QTableView
.
Is there something like QTableWidgetItem
that I am able to use it in QTableView
?
I use QSqlTableModel
.
I'm curious about how I can display an image from my database in a QTableView
.
Is there something like QTableWidgetItem
that I am able to use it in QTableView
?
I use QSqlTableModel
.
A rough idea is to use
QStandardItem::setData
to set aQPixmap
(transformed intoQVariant
) on it, then you can set theQStandardItem
on theQStandardItemModel
.Sequence:
QImage
--->QPixmap
--->QVariant
--->QStandardItem
--->QStandardItemModel
For example:
You will have to resize images or cell size, depends on what you need.
[Edit]
If you are using
QSqlTableModel
, just keep using it. All we need to do is make those path strings intoQPixmap
and set the item role to beQt::DecorationRole
in that column.As the document says:
To do this, the concept is simple: provide
QTableView
withQVariant
ofQPixmap
asQTableView
render them according toQt::DecorationRole
.You may subclass
QSqlTableModel
and reimplement the virtual functionQVariant data(const QModelIndex & index, int role = Qt::DisplayRole)
and make the image column return theQPixmap
asQVariant
, with the decoration role. So do something like this:Besides, you can also try subclassing
QStyledItemDelegate
and reimplementpaint()
function to customize your own delegate, but that will require a more complicated work. An example using delegate can be found here. You can paint whatever you want with delegate, even a button.*Sorry the code is not tested, as I don't have a database on hand.