Unsolved QStiledItemDelegate setModelData doesn't display image
-
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.
-
either properly split declarations and definitions between .h and .cpp or just leave everything in the .h file and delete extrarolesproxymodel.cpp altogether. The compilation process takes a bit longer but your compiler should be smart enough to avoid any difference in performance from one to the other in release mode
-
@VRonin
Now I'm just using the code in the .h file and there is no error.I have the
if(role == Qt::EditRole) return QIdentityProxyModel::setData(index, value, role);
line after
if(!index.isValid()) return false;
(that is line 29) and I pushed the original line 29 one line down. Was that the correct way?
At this point no error message, but no image displayed either. Do I need to link the proxy to fixdb.cpp or fixviewdelegate .cpp? -
@VRonin
How can I connect the proxy to my oriinal model or fixdb? -
-
@VRonin
Is this supposed to go in the ExtraRolesProxyModel?