Showing pictures in a tableView by reading from a database?
-
I have a tableView whose code is:
qry.prepare("select * from movie "); if (qry.exec()) { model->setQuery(qry); ui->tableView->setModel(model); }
But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this? -
I have the following code to store my image in the db:
QPixmap *inPixmap = new QPixmap(filename); QByteArray inByteArray; QBuffer inBuffer(&inByteArray); inBuffer.open(QIODevice::WriteOnly); inPixmap->save(&inBuffer, "PNG"); QSqlQuery query = QSqlQuery(db)
Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.
@RootLearner Oops, I forgot to point out that you have to add:
ui->tableView->setItemDelegateForColumn(column1, new ImageDelegate); ui->tableView->setItemDelegateForColumn(column2, new ImageDelegate);
-
I have a tableView whose code is:
qry.prepare("select * from movie "); if (qry.exec()) { model->setQuery(qry); ui->tableView->setModel(model); }
But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this?@RootLearner Are the images stored in raw or base64 format?
-
I have a tableView whose code is:
qry.prepare("select * from movie "); if (qry.exec()) { model->setQuery(qry); ui->tableView->setModel(model); }
But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this?@RootLearner You can use a delegate:
class ImageDelegate: public QStyledItemDelegate{ public: using QStyledItemDelegate::QStyledItemDelegate; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ const QByteArray & bytes = index.data().toByteArray(); QPixmap pixmap; if(!bytes.isNull() && pixmap.loadFromData(bytes)){ // for BASE64 change to // pixmap.loadFromData(QByteArray::fromBase64(bytes)) painter->drawPixmap(option.rect, pixmap.scaled(option.rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); return; } return QStyledItemDelegate::paint(painter, option, index); } };
-
I have the following code to store my image in the db:
QPixmap *inPixmap = new QPixmap(filename); QByteArray inByteArray; QBuffer inBuffer(&inByteArray); inBuffer.open(QIODevice::WriteOnly); inPixmap->save(&inBuffer, "PNG"); QSqlQuery query = QSqlQuery(db)
Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.
-
I have the following code to store my image in the db:
QPixmap *inPixmap = new QPixmap(filename); QByteArray inByteArray; QBuffer inBuffer(&inByteArray); inBuffer.open(QIODevice::WriteOnly); inPixmap->save(&inBuffer, "PNG"); QSqlQuery query = QSqlQuery(db)
Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.
@RootLearner Oops, I forgot to point out that you have to add:
ui->tableView->setItemDelegateForColumn(column1, new ImageDelegate); ui->tableView->setItemDelegateForColumn(column2, new ImageDelegate);