Results 1 to 6 of 6

Thread: Access from Delegate to Model

  1. #1
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. void SchulungsplanDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex &index) const
    2. {
    3. m_spModel = static_cast<SchulungsplanModel>(index.model() );
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access from Delegate to Model

    The index returns a pointer to the model. Your cast is incorrect, should be:
    Qt Code:
    1. SchulungsplanModel* model = qobject_cast<SchulingspanModel*>(index.model());
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to wysota for this useful post:

    bunjee (22nd April 2008), starcontrol (11th April 2008)

  4. #3
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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...

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  6. #5
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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??

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access from Delegate to Model

    Use const_cast or cast to const SchulungsplanModel*.

Similar Threads

  1. QAbstractItemModel with QUndoStack
    By No-Nonsense in forum Qt Programming
    Replies: 7
    Last Post: 28th October 2012, 17:15
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  3. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  4. Model View - Delegate - setIndexWidget
    By starcontrol in forum Qt Programming
    Replies: 16
    Last Post: 2nd April 2008, 14:30
  5. Model Choices review/questions
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2007, 21:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.