Access from Delegate to Model
Hey all,
do you know, how I can access the model (subclassed from QAbstractTableModel) from my delegate class? How do I get a pointer from the delegate class to my subclassed model? I subclassed QItemDelegate for my delegate...
I tried something like this:
Code:
void SchulungsplanDelegate
::paint(QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex &index
) const {
m_spModel = static_cast<SchulungsplanModel>(index.model() );
bit this does not work, because(failure message):
no matching function for call to `SchulungsplanModel::SchulungsplanModel(const QAbstractItemModel*)' 200308 SchulungsplanDelegate.cpp line 25 1207902734474 1013
Re: Access from Delegate to Model
The index returns a pointer to the model. Your cast is incorrect, should be:
Code:
SchulungsplanModel* model = qobject_cast<SchulingspanModel*>(index.model());
Re: Access from Delegate to Model
ok, but should it not be better to instance the model pointer only once in the constructor instead of the paint method, which will be executed endlessly?
In the constructor I dont't have the index params...
Re: Access from Delegate to Model
It is only a pointer, you don't instantiate anything. A single delegate might be working with more than one model thus it would be dangerous to do what you suggest. Furthermore the delegate is tied to the view, not to the model.
Re: Access from Delegate to Model
...so i tried your code suggestion, but now i get this error:
c:/programme/Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qobject.h static_cast from type `const QObject*' to type `SchulungsplanModel*' casts away constness 200308 line 442 1207906858745 1045
...because (index.model()) returns a const pointer, how do I have to cast correctly??
Re: Access from Delegate to Model
Use const_cast or cast to const SchulungsplanModel*.