Displaying image in QSqlTableModel
-
Hi,
I have a QSqlTableModel and I want to use it to display an image from a databasecin a TableView. I have the following code so far:#include "fixdb.h" #include "ui_fixdb.h" #include <QDebug> FixDb::FixDb(QWidget* parent) : QDialog(parent), ui(new Ui::FixDb) { ui->setupUi(this); correctFriend (); } FixDb::~FixDb() { delete ui; } void FixDb::correctFriend() { QSqlQuery query_fix; int recNum = 0; query_fix.prepare ("SELECT COUNT(*) FROM Items"); query_fix.exec (); if(query_fix.next ()) { recNum = query_fix.value (0).toInt (); }; qDebug() << "Record count in fixdb: " << recNum; QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("C:/Programming/Projects/Folkfriends_bzr/trunk/db.db"); if(!db.open ()) QMessageBox::information (this, "Error", "Couldn't open database."); QSqlTableModel* fixModel = new QSqlTableModel(this); fixModel->setTable ("Items"); fixModel->setEditStrategy (QSqlTableModel::OnFieldChange); fixModel->setSort (1, Qt::DescendingOrder); fixModel->select (); ui->tableView_Fix->setModel (fixModel); QSqlQuery fixQueryDisp; fixQueryDisp.prepare("SELECT * FROM Items"); fixQueryDisp.exec (); for(int row1 = 0; row1 < recNum; row1++) { ui->tableView_Fix->setRowHeight (row1, 100); while (fixQueryDisp.next ()) { fixPixmap.loadFromData (fixQueryDisp.value (2).toByteArray ()); fixPixmap = fixPixmap.scaled (100, 100, Qt::KeepAspectRatio); fixModel->setData (fixModel->index (row1, 3), fixPixmap, Qt::DecorationRole); } }; ui->tableView_Fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold; text-decoration: underline;}"); ui->tableView_Fix->verticalHeader ()->setVisible (false); ui->tableView_Fix->setAlternatingRowColors (true); ui->tableView_Fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial; "); fixModel->setHeaderData (0, Qt::Horizontal, QObject::tr ("ID")); fixModel->setHeaderData (1, Qt::Horizontal, QObject::tr ("Name")); fixModel->setHeaderData (3, Qt::Horizontal, QObject::tr ("What")); fixModel->setHeaderData (2, Qt::Horizontal, QObject::tr ("Image")); fixModel->setHeaderData (4, Qt::Horizontal, QObject::tr ("Material")); fixModel->setHeaderData (5, Qt::Horizontal, QObject::tr ("Color")); fixModel->setHeaderData (6, Qt::Horizontal, QObject::tr ("Description")); fixModel->setHeaderData (7, Qt::Horizontal, QObject::tr ("Adoption Date")); fixModel->setHeaderData (8, Qt::Horizontal, QObject::tr ("Signed by")); fixModel->setHeaderData (9, Qt::Horizontal, QObject::tr ("History")); fixModel->setHeaderData (10, Qt::Horizontal, QObject::tr ("Age")); fixModel->setHeaderData (11, Qt::Horizontal, QObject::tr ("Notes")); ui->tableView_Fix->setColumnWidth (0, 60); ui->tableView_Fix->setColumnWidth (1, 120); ui->tableView_Fix->setColumnWidth (2, 150); ui->tableView_Fix->setColumnWidth (3, 100); ui->tableView_Fix->setColumnWidth (4, 130); ui->tableView_Fix->setColumnWidth (5, 120); ui->tableView_Fix->setColumnWidth (6, 400); ui->tableView_Fix->setColumnWidth (7, 150); ui->tableView_Fix->setColumnWidth (8, 200); ui->tableView_Fix->setColumnWidth (9, 400); ui->tableView_Fix->setColumnWidth (10, 130); ui->tableView_Fix->setColumnWidth (11, 300); ui->tableView_Fix->setWordWrap (true); }//end of correctFriend
There is no error message, but the images are not displayed. The image column is #3. Please help me find what I missed.
-
Don't set the data, QSqlTableModel is read only for any role other than Qt::EditRole, subclass QStyledItemDelegate, reimplement paint() to take the QByteArray and paint the QPixmap, then just set the delegate to the last column
-
@VRonin
I tried to implement the code at the link, so I createdbool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
in fixdb.h and
bool FixDb::QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role) { }
in fixdb.cpp. I got he following error message:
C:\Programming\Projects\Folkfriends_bzr\checkout\fixdb.h:39: error: cannot declare member function 'QSqlTableModel::setData' within 'FixDb'
bool QSqlTableModel::setData(const QModelIndex& index, const QVariant& value, int role);
^
How can I avoid this error? -
@gabor53
Hi
As it says. You cannot create a member function for one class in another class
FixDb::QSqlTableModel::setDataTo override setData the class must be a child of QSqlTableModel. so not sure what you try to to do.
As @VRonin says, you need to use a QStyledItemDelegate to draw the image
this one is good example
http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html