0

I stacked in a problem which is displaying images in each row in photo's column from SQLite Database

Here is my code and thanks in advance:

admin.cpp

void Admin::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    QSqlQueryModel *modal = new QSqlQueryModel();
    this->model = new QSqlQueryModel();
    model->setQuery("select * from professeur");
      // qDebug()<<model->lastError();
        ui->tableView_prof->setModel(model);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    How are the images stored in the database? Could you share the database? – eyllanesc Nov 18 '18 at 01:37
  • Yeah sure they are stocked as blob. Here is my database's table: CREATE TABLE "professeur" ( `Photo` BLOB, `Nom et prenom de professeur` TEXT, `Telephone` TEXT, `Sexe` TEXT, `E-mail` TEXT, `Carte d'identité` TEXT, `Date d'éxpédition CIN` TEXT, `Lieu d'expedition CIN` TEXT, `Adresse` TEXT, `Prix heure` TEXT, `Banque` TEXT, `RIB` TEXT, `Description` TEXT, `Activation` TEXT ) – Good Programmer Nov 18 '18 at 20:45
  • please share your .db – eyllanesc Nov 18 '18 at 20:48
  • thanks @eyllanesc I solved my problem. So I subclass QSqlQueryModel and I displayed the blob file as image here is my code – Good Programmer Nov 18 '18 at 21:20

1 Answers1

0

I solved my problem here is the code

QVariant imageshow::data(const QModelIndex &item, int role) const
{
// Column for image
if (item.column() == 0)
{
    // avoid showing image as text
    if (role == Qt::DisplayRole || role == Qt::EditRole)
        return QVariant();
    //show the qpixmap in the column
    if (role == Qt::DecorationRole)
    {
        QVariant variant(QSqlQueryModel::data(item, Qt::DisplayRole));
        QByteArray bytes(variant.toByteArray());
        QPixmap pixmap;
        pixmap.loadFromData(bytes, "png");
        return pixmap;
    }
}
return QSqlQueryModel::data(item, role);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459