Unsolved Reading image from QSqlTableModel
-
Hi, apparently loadFromData fails.
You have hardcoded fixModel->record(1) - are you sure the data is there? In row number one?
Rows are numbered in zero-based manner.
Other problem might be that field "Pic" returns invalid value. -
Hi @artwaw ,
The data is there. There is an image in each record in the Pic field. Is it the correct way I'm trying to load the image?
Thank you. -
@gabor53 Actually in the method you posted here there should not be any hardcoded values. I would post rather elaborate answer a bit later in the evening, I am commuting now.
-
@artwaw
Thank you. I would appreciate that. Also I assigned the delegate to the view like this:QSqlTableModel* fixModel = new QSqlTableModel(this); fixModel->setTable ("Items"); fixModel->setEditStrategy (QSqlTableModel::OnFieldChange); fixModel->setSort (2, Qt::DescendingOrder); fixModel->select (); ui->tableView_Fix->setItemDelegateForColumn(3, new FixViewDelegate (this));
The goal would be to get the delegate draw the image each time.
-
Hi,
How did you store the image data in your database ?
-
Hi @SGaist ,
I store them as a QByteArray:QPixmap pix2(fileNameChosen); int w = 0; int h = 0; w = ui->Image_Display_Label->width (); h = ui->Image_Display_Label->height (); ui->Image_Display_Label->setPixmap (pix2.scaled(w, h, Qt::KeepAspectRatio)); QFile fileReview(fileNameChosen); if(fileReview.open (QIODevice::ReadOnly)) { fileByteArray = fileReview.readAll ();
-
-
Hi @jsulm ,
It's BLOB. -
@gabor53 ,
What is the size of the bytearray?
-
@Vinod-Kuntoji
100 x 100 -
@gabor53 ,
The blob you are using, is it a raw bytearray or png, jpeg byterray? -
@Vinod-Kuntoji
It is jpeg bytearray. -
@gabor53 ,
Then you should specify "JPG" in loadFromData function.
QPixmap fixPic;
fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (),"JPG"); -
@Vinod-Kuntoji
Thank you. I've noticed an other issue: the program execution enters fixviewdelegate.cpp but never reads the actual function:void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const { Q_UNUSED(index); Q_UNUSED(editor); qDebug() << "Entered fixViewDelegate (2)."; QPixmap fixPic; fixPic.loadFromData (fixModel->record (1).value ("Pic").toByteArray (), "JPG"); qDebug() << "Fixmodel pic: " << fixPic; qDebug() << "Fixmodel pic size: " << fixPic.size (); }
What can I do to fix this problem?
-
-
@gabor53
what is output of the following code in the console:qDebug() << QImageReader::supportedImageFormats();
Also on what system are you running your application?
-
Hi @raven-worx ,
The output is
("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm").
I run Qt 5.8 on Windows 10. -
@gabor53
ok that at least ensures you can read jpg images.
Now the only possibility is that the data you retrieve from the database isn't valid imagedata.QByteArray ba = fixModel->record (1).value ("Pic").toByteArray (); qDebug() << ba.size() << ba;
-
@raven-worx
I added it to thevoid FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const {
function but it doesnt display anything. I know program execution goes from fixdb.cpp to FixViewDelegate, but it does nothing with setModelData. As this delegate supposed to handle displaying the image without clicking on the table I think something is missing. How can I render the image using this delegate each time the tableView is displayed?
The delegate so far looks like this:#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(painter); Q_UNUSED(option); Q_UNUSED(index); } QSize FixViewDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(option); Q_UNUSED(index); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { Q_UNUSED(editor); Q_UNUSED(index); } void FixViewDelegate::setModelData(QWidget* editor, QSqlTableModel* fixModel, const QModelIndex& index) const { Q_UNUSED(index); Q_UNUSED(editor); qDebug() << "Entered fixViewDelegate (2)."; QByteArray ba = fixModel->record (1).value ("Pic").toByteArray (); qDebug() << ba.size() << ba; // qDebug() << "Fixmodel pic: " << fixPic; // qDebug() << "Fixmodel pic size: " << fixPic.size (); }
Thank you for your help.
-
@gabor53
2 reasons your csetModelData() isn't called:- the correct signatur for setModelData() would be:
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
setModelData()
is used as the name implies to set data in the model. Means when the editor has finished editing and wants to save
the correct method to reimplement is the delegates
paint()
andsizeHint()
methods. In there you check if the column is your image column and do the work appropriately, else just call the base class implementation. - the correct signatur for setModelData() would be: