Results 1 to 4 of 4

Thread: Cannot cast internal pointer of QModelIndex to custom class pointer

  1. #1
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Cannot cast internal pointer of QModelIndex to custom class pointer

    Hello,
    I have a QAbstractListModel subclass which has a QList with objects of type "struct Foo". For a custom paint job, I use QStyledItemDelegates for the view. Inside its paint() method, I'd like to handle the real object behind the item directly. So I tried getting it by casting the internal pointer of the model index. This will compile, but only yield 0.

    Qt Code:
    1. Foo *f = static_cast<Foo*>(index.internalPointer());
    To copy to clipboard, switch view to plain text mode 

    The problem here is, that the internal pointer is already 0. In the past I successfully used static casts on internal pointers.

    On the other hand, using the model from the index and a custom method in the model works quite well. Yet, this solution looks rather long-winded.

    Qt Code:
    1. Foo *f = ((FooModel*)index.model()).fooByIndex(index);
    2.  
    3. // Model definiton (excerpt)
    4. class FooModel : public QAbstractListModel
    5. {
    6. public:
    7. Foo *fooByIndex(const QModelIndex &index);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Am I missing something here or is the internal pointer supposed to be null?


    Andreas

  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: Cannot cast internal pointer of QModelIndex to custom class pointer

    I would guess the index you are calling the function on is already invalid.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot cast internal pointer of QModelIndex to custom class pointer

    Or you never put a Foo* in the QModelIndex through your createIndex() calls.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cannot cast internal pointer of QModelIndex to custom class pointer

    Quote Originally Posted by antimatter View Post
    So I tried getting it by casting the internal pointer of the model index.
    That is very bad in any way, it is called "internal" for a reason. Only the model should ever make assumption about it because it is the one creating the index.

    I would recommend adding a role that returns the full model data entry

    Qt Code:
    1. Foo foo = index.data(FooRole).value<Foo>();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 14th June 2013, 05:59
  2. Can I cast a pointer from a unknow type ?
    By john_god in forum General Programming
    Replies: 13
    Last Post: 30th May 2011, 13:59
  3. Replies: 1
    Last Post: 4th December 2010, 18:20
  4. deleting internal pointer in QModelIndex
    By rickbsgu in forum Qt Programming
    Replies: 18
    Last Post: 24th December 2008, 03:24
  5. Replies: 2
    Last Post: 16th February 2006, 20:09

Tags for this Thread

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.