Display images from sqlite database to a column in QTableView

739 views Asked by At

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);
}
1

There are 1 answers

0
Good Programmer On BEST ANSWER

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);
}