PDA

View Full Version : How to show images from database as icons in QListView



LeshaS
18th January 2011, 11:39
Hi guys,

I have a database which stores images as BLOB field.
What I'm trying to achieve is to show those images (thumbnails of them) as icons in QListView. I use QSqlQueryModel as a model to access the database.

Can anyone give me any idea how to implement that? If I chose wrong classes, can you suggest better ones for that purpose?

Thanks,
Alex

wysota
18th January 2011, 16:32
Is there anything in particular you are having problems with?

LeshaS
21st January 2011, 08:13
Hi wysota, thanks for reply.
I just had no idea how to implement it.
Now I have done it the following way:

I've created a new model class based on QSqlQueryModel and implemented data() method:



QVariant SqlQueryModel::data ( const QModelIndex & item, int role) const
{
if (role == Qt::DecorationRole)
{
switch(item.column())
{
case 0: // column 0 contains text data
QPixmap image;
image.loadFromData(record(item.row()).value(1).toB yteArray()); // column 1 contains BLOB data
return image.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
return QSqlQueryModel::data(item, role);
}

Then I connected it to QListView like this:


m_pQueryModel = new SqlQueryModel(this);
m_pQueryModel->setQuery(m_sQuery, m_sqlDatabase);
QListView *pView = new QListView(ui.tab_2);
pView->setViewMode(QListView::IconMode);
pView->setModelColumn(0);
pView->setModel(m_pQueryModel);


I'm curious whether it's correct solution or it can be improved somehow.
Thanks,
Alex