I'm trying to implement my own model/delegate where the model returns pointers to a QHash<QString, QString> *. I've called Q_DECLARE_METATYPE in order to register the new type and have also called qRegisterMetaType<MyType>("MyType"). Unfortunately, when I try to convert the call to index.data(Qt:isplayType) into <QHash<QString, QString> *, all I seem to get back is a null pointer...

Qt Code:
  1. typedef QHash<QString, QString>* MyType;
  2. Q_DECLARE_METATYPE(MyType)
  3.  
  4. class MyDelegate : public QItemDelegate
  5. {
  6. Q_OBJECT
  7.  
  8. public:
  9. MyDelegate(QWidget *parent = 0);
  10.  
  11. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
  12. const QModelIndex &index) const;
  13. };
To copy to clipboard, switch view to plain text mode 

So the declaration above helped me get the code to compile.

Qt Code:
  1. MyDelegate::MyDelegate(QWidget *parent)
  2. :QItemDelegate(parent)
  3. {
  4. qRegisterMetaType<MyType>("MyType");
  5. }
  6.  
  7. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  8. const QModelIndex &index) const
  9. {
  10. QRect rect = option.rect;
  11.  
  12. QHash<QString, QString> * t = 0;
  13. t = qVariantValue<QHash<QString, QString>*>(index.data(Qt::DisplayRole));
  14. if(t != 0)
  15. {
  16. std::cout << "T size: " << t->size() << std::endl;
  17. }
  18.  
  19. if(option.state & QStyle::State_Selected)
  20. {
  21. painter->fillRect(rect, option.palette.highlight());
  22. }
  23. else
  24. {
  25. QItemDelegate::paint(painter, option, index);
  26. }
  27. }
To copy to clipboard, switch view to plain text mode 

Now something is going wrong on lines 12 and 13. Perhaps something about my data declaration below is incorrect? Of course I'm expecting that I should now have a handle on that index in the model. If I did, then I would proceed to format everything in the paint call.

Qt Code:
  1. QVariant CustomModel::data(const QModelIndex &index, int role)const
  2. {
  3. if(!index.isValid())
  4. {
  5. return QVariant();
  6. }
  7. if(role == Qt::TextAlignmentRole)
  8. {
  9. return int(Qt::AlignRight | Qt::AlignVCenter);
  10. }
  11. else if (role == Qt::DisplayRole)
  12. {
  13. //Returning a pointer to the QHash<QString, QString> so just
  14. //cast back and then use the data in the view.
  15. return tableData.at(index.row());
  16. }
  17. return QVariant();
  18. }
To copy to clipboard, switch view to plain text mode 

Thanks for taking your time