View Full Version : Access from Delegate to Model

11th April 2008, 10:37
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:

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

11th April 2008, 10:50
The index returns a pointer to the model. Your cast is incorrect, should be:

SchulungsplanModel* model = qobject_cast<SchulingspanModel*>(index.model());

11th April 2008, 10:57
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...

11th April 2008, 11:01
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.

11th April 2008, 11:44
...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??

11th April 2008, 13:26
Use const_cast or cast to const SchulungsplanModel*.