QStiledItemDelegate setModelData doesn't display image
-
Hi,
My fixviewdelegate.cpp looks like this:#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Entered createEditor"; Q_UNUSED(option); Q_UNUSED(index); return new ImagePickButton(parent); } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap loadedImage; loadedImage.loadFromData (index.data ().toByteArray ()) ; loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio); const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage); } void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qDebug() << "Entered setEditorData."; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); } void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); QPixmap newImage(value); newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; fixModel->setData(index, newImage, Qt::DecorationRole); }
The setModelData has the newImage pixmap (qDebug displays the size correctly) and the index is correct and valid. The new image appears on the editor, but it is not written back to the model (the model is not updated). Is it because
-
I am using a wrong image format,
-
I am not supposed to do this in setModelData,
-
something is missing,
-
or something else is incorrect?
Thank you for your help.
-
-
Hi,
Does your model handle properly the Qt::DecorationRole ?
-
What type of model does your view use ?
-
@SGaist
QSqlTableModelI modified setModelData like this:
if(fixModel->setData(index, newImage, Qt::EditRole) == true) { qDebug() << "Data IS set. "; } else { qDebug() << "Data is NOT set. "; }
It gave the Data IS set message, but the new image still doesn't show in the tableView managed by the model.
-
Except that you changed the role used between your different answers so it doesn't make things any clearer.
Can you post the complete code you are actually using ?
-
@SGaist
Sorry for the confusion.void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); QPixmap newImage(value); newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; if(fixModel->setData(index, newImage, Qt::EditRole) == true) { qDebug() << "Data IS set. "; } else { qDebug() << "Data is NOT set. "; } }
DecorationRole gives Data is NOT set.
-
I feel like you are a bit confused about roles.
- in
FixViewDelegate::paint
,loadedImage.loadFromData (index.data ().toByteArray ()) ;
takes the data fromQt::DisplayRole
(default argument todata()
) as a byte-array as it was an encoded pixmap - in
FixViewDelegate::setEditorData
,imgPick->setSelectedFile(index.data(Qt::UserRole).toString());
takes the path to the image fromQt::UserRole
- in
FixViewDelegate::setModelData
you directly save the QPixmap infixModel->setData(index, newImage, Qt::DecorationRole);
So the answer to your question is chose 1 way to save and load your image and use it across all your methods.
This is what I'd do to fix your code:
- add
fixModel->setData(index, value, Qt::UserRole);
as the last line ofFixViewDelegate::setModelData
- in
FixViewDelegate::paint
removeloadedImage.loadFromData (index.data ().toByteArray ()) ;
- in
FixViewDelegate::paint
changeQPixmap loadedImage;
toQPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>();
Optional but encouraged:
- add a check for
value
being empty and/ornewImage
being null inFixViewDelegate::setModelData
- in
-
@VRonin
I made the following changes as you recommended:#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Entered createEditor"; Q_UNUSED(option); Q_UNUSED(index); return new ImagePickButton(parent); } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap loadedImage; loadedImage = index.data (Qt::DecorationRole).value<QPixmap>(); loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio); const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage); } void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qDebug() << "Entered setEditorData."; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); } void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); if(value == "") { qDebug() << "value is empty."; } else { qDebug() << "value is NOT empty."; } QPixmap newImage(value); newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; if(fixModel->setData(index, value, Qt::UserRole) == true) { qDebug() << "Data IS set. "; } else { qDebug() << "Data is NOT set. "; } }
paint stopped working. There is no image in the tableView's image column.
-
@VRonin
I have the following code (hopefully I made all the changes correctly):#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Entered createEditor"; Q_UNUSED(option); Q_UNUSED(index); return new ImagePickButton(parent); } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap loadedImage = index.data (Qt::DecorationRole).value<QPixmap>(); loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio); const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage); } void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qDebug() << "Entered setEditorData."; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); } void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); if(value == "") { qDebug() << "value is empty."; } else { qDebug() << "value is NOT empty."; } QPixmap newImage(value); newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; if(fixModel->setData(index, newImage, Qt::DecorationRole) == true) { qDebug() << "Data IS set. "; } else { qDebug() << "Data is NOT set. "; } }
I still has the same problems:
-
fixViewDelegate::paint doesn't display the images
-
fixViewDelegate::setData says
value is NOT empty.
newImage size: QSize(75, 100)
Index: QModelIndex(2,2,0x0,QSqlTableModel(0x2130a3f0))
Data is NOT set. and the chosen image is not displayed.
Thank you for your help.
-
-
Like I already wrote somewhere else: QSqlTableModel doesn't handle Qt::Decoration in its setData implementation (or rather it calls the base class implementation and following up that chain, it ends with nothing done) so if you want to use it you have to handle it yourself.
Otherwise, you can use the EditRole as usual to store the data and paint the image when the decoration role is requested..
-
@SGaist
I changed setModelData:void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); QPixmap newImage(value); newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; if(fixModel->setData(index, newImage, Qt::EditRole) == true) { qDebug() << "Data IS set. "; } else { qDebug() << "Data is NOT set. "; } }
Everything looks ok (newImage exists and Data is set), but it still doesn't paint the image and it is not saved in the db either. Do I need to implement saving and painting, or something is still incorrect?
-
Apologies, I did not read the model was QSqlTableModel. You can put a proxy like this one (You'll have to add
if(role==Qt::EditRole) return QIdentityProxyModel::setData(index,value,role);
in line 29) in between to make the multiple roles work. On the other hand, you latest implementations ofsetModelData
look like you do not completely understand how models work and you are panicking and try to mix a bunch of stuff together hoping that it works#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Entered createEditor"; Q_UNUSED(option); Q_UNUSED(index); return new ImagePickButton(parent); } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap loadedImage=index.data(Qt::DecorationRole).value<QPixmap>(); const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage); } void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qDebug() << "Entered setEditorData."; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); } void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); QPixmap newImage(value); if(newImage.isNull()){ fixModel->setData(index, QVariant(), Qt::DecorationRole); fixModel->setData(index, QVariant(), Qt::UserRole); return; } newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; fixModel->setData(index, newImage, Qt::DecorationRole); fixModel->setData(index, value, Qt::UserRole); }
You will still need to use the proxy I linked above inbetween
-
@VRonin
Thank you.
I created a class named ExtraRolesProxyModel.
I added#include "extrarolesproxymodel.h"
to fixviewdelegate.h.
My fixviewdelegate.cpp looks like this now:#include "fixviewdelegate.h" FixViewDelegate::FixViewDelegate(QObject* parent) : QStyledItemDelegate(parent) { qDebug() << "Entered fixViewDelegate (1)."; } QWidget* FixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Entered createEditor"; Q_UNUSED(option); Q_UNUSED(index); return new ImagePickButton(parent); } void FixViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { if(role == Qt::EditRole) return; QIdentityProxyModel::setData(index, value, role); QPixmap loadedImage = index.data (Qt::DecorationRole).value<QPixmap>(); // loadedImage.loadFromData (index.data ().toByteArray ()) ; // loadedImage = loadedImage.scaled (100, 100, Qt::KeepAspectRatio); const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); style->drawItemPixmap (painter, option.rect, Qt::AlignCenter, loadedImage); } void FixViewDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { Q_UNUSED(index); editor->setGeometry(option.rect); } void FixViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qDebug() << "Entered setEditorData."; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); imgPick->setSelectedFile(index.data(Qt::UserRole).toString()); } void FixViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* fixModel, const QModelIndex& index) const { qDebug() << "Entered setModelData. "; ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor); Q_ASSERT(imgPick); QString value; value = imgPick->selectedFile (); QPixmap newImage(value); if(newImage.isNull ()) { fixModel->setData (index, QVariant(), Qt::DecorationRole); fixModel->setData (index, QVariant(), Qt::UserRole); return; } newImage = newImage.scaled (100, 100, Qt::KeepAspectRatio); qDebug() << "newImage size: " << newImage.size (); qDebug() << "Index: " << index; // if(fixModel->setData(index, newImage, Qt::EditRole) == true) { // qDebug() << "Data IS set. "; // } else { // qDebug() << "Data is NOT set. "; // }; fixModel->setData (index, newImage, Qt::DecorationRole); fixModel->setData (index, newImage, Qt::UserRole); }
When I build it it gives the following errors:
'role' was not declared in this scope.
'value' was not declared in this scope.
Did I add the proxy incorrectly? -
That's a proxy model, it has nothing to do with the delegate. I have no idea why you merged the 2. I gave you the code of both the delegate and the proxy model. You just have to copy-paste them, add 1 single line I posted at the place I posted inside
extrarolesproxymodel.h
it and then use them -
@VRonin
Isif(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role);
goes into extrarolesproxymodel.h or cpp?
There are no line 29 in .h. If it goes to the cpp file it replaces line 29, goes before or after?
I tried to add it before and afterif(!index.isValid()) return false;
in cpp. Tried to place it before and after line 29 and actually replaced line 29 but still images were not displayed.
-
@gabor53 said in QStiledItemDelegate setModelData doesn't display image:
goes into extrarolesproxymodel.h or cpp?
The link I posted is header only so I don't know what you are referring to. Use the line numbering you see in pastebin to understand where to put it
-
@VRonin
extraroleproxymodel.h looks like this:#include <QIdentityProxyModel> #include <QVariant> #include <QHash> class ExtraRolesProxyModel : public QIdentityProxyModel { Q_OBJECT Q_DISABLE_COPY(ExtraRolesProxyModel) public: ExtraRolesProxyModel(QObject* parent = Q_NULLPTR) : QIdentityProxyModel(parent) { } virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE { if(!index.isValid()) return QIdentityProxyModel::data(index, role); const QHash<int, QHash<int, QHash<int, QVariant> > >::const_iterator rowIter = m_extraData.constFind(index.row()); if(rowIter == m_extraData.cend()) return QIdentityProxyModel::data(index, role); const QHash<int, QHash<int, QVariant> >::const_iterator colIter = rowIter->constFind(index.column()); if(colIter == rowIter->cend()) return QIdentityProxyModel::data(index, role); const QHash<int, QVariant>::const_iterator roleIter = colIter->constFind(role); if(roleIter == colIter->cend()) return QIdentityProxyModel::data(index, role); return roleIter.value(); } virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)Q_DECL_OVERRIDE{ if(!index.isValid()) return false; if(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role); if(index.parent().isValid()) return QIdentityProxyModel::setData(index, value, role); QHash<int, QHash<int, QHash<int, QVariant> > >::iterator rowIter = m_extraData.find(index.row()); if(rowIter == m_extraData.end()) { if(value.isNull()) return false; rowIter = m_extraData.insert(index.row(), QHash<int, QHash<int, QVariant> >()); } QHash<int, QHash<int, QVariant> >::iterator colIter = rowIter->find(index.column()); if(colIter == rowIter->end()) { if(value.isNull()) return false; colIter = rowIter->insert(index.column(), QHash<int, QVariant>()); } QHash<int, QVariant>::iterator roleIter = colIter->find(role); if(roleIter == colIter->end()) { if(value.isNull()) return false; roleIter = colIter->insert(role, value); return true; } if(value.isNull()) { colIter->erase(roleIter); if(colIter->isEmpty()) { rowIter->erase(colIter); if(rowIter->isEmpty()) { m_extraData.erase(rowIter); } } return true; } roleIter.value() = value; return true; } private: QHash<int, QHash<int, QHash<int, QVariant> > > m_extraData; };
It gives the following errors:
C:\Programming\Projects\Folkfriends_bzr\checkout\extrarolesproxymodel.cpp:5: error: redefinition of 'QVariant ExtraRolesProxyModel::data(const QModelIndex&, int) const'
QVariant ExtraRolesProxyModel::data(const QModelIndex& index, int role) const {
^
C:\Programming\Projects\Folkfriends_bzr\checkout\extrarolesproxymodel.cpp:21: error: redefinition of 'bool ExtraRolesProxyModel::setData(const QModelIndex&, const QVariant&, int)'
bool ExtraRolesProxyModel::setData(const QModelIndex& index, const QVariant& value, int role) {
^
Am I still doing something incorrectly?
Thank you. -
Like the error suggests: you have the functions implemented twice. Once in the header and once in the .cpp file.