[Qt5-feedback] Add internalVariant to QModelIndex

Stephen Kelly steveire at gmail.com
Sat Jul 16 11:30:13 CEST 2011


Stephen Kelly wrote:

> QVariant var = item.data();
> 
> if (var.type() == BookType)
> MyBook book = var.value<MyBook>();
> // ... Use book.
> else if (var.type() == ChapterType)
> MyChapter chapter = var.value<MyChapter>();
> // ... Use chapter
> else if (var.type() == ParagraphType) ...

Just for clarity, though you may have already realised, what I wrote is not 
correct. In fact, you don't even need the enum:


QVariant var = item.data();

if (var.type() == qMetaTypeId<MyBook*>())
  MyBook *book = var.value<MyBook*>();
  // ... Use book.
else if (var.type() == qMetaTypeId<MyChapter*>())
  MyChapter* chapter = var.value<MyChapter*>();
  // ... Use chapter
else if (var.type() == qMetaTypeId<MyParagraph*>()) ...


Assuming you store pointers in the QVariant.

But that's going off topic for this thread now.

Thanks,

Steve.



More information about the Qt5-feedback mailing list