Hi everyone,
I'm fairly new to Qt so i'm pretty sure i'm taking the long way around to solv a very simple problem. Anyhow, what I want to do is to obtain is a QListWidget in which under every image is a description of the image, which can be edited in a QTextBox, thus allowing multiple lines and wordwrap.
What I did was to create a new QItemDelegate subclassing from QStyledItemDelegate (ItemDelegate), and then I created a editor subclassing from QWidget (DelegateWidget).
The problem I am having right now is that when I pass the QModelIndex to the DelegateWidget, I can't get the Image from the DecorativeRole.
Can someone tell me what I'm doing wrong, or if there is an easier way, as I really think I'm just making a mess.
P.S.
I was actually thinking I might just create a ScrollArea from scratch and just add the images and text to the ScrollArea, but then I'd probably hav to reimplement the dragging etc.

QItemDelegate
Qt Code:
  1. ItemDelegate::ItemDelegate(QObject *parent)
  2. : QStyledItemDelegate(parent)
  3. {
  4. }
  5.  
  6. QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index ) const
  7. {
  8.  
  9.  
  10. return new DelegateWidget(parent, index);
  11. }
  12.  
  13. void ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  14. {
  15.  
  16. static_cast<DelegateWidget*>(editor)->setText(index.data(Qt::EditRole).toString());
  17. }
  18.  
  19. void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
  20. {
  21.  
  22. QString value=static_cast<DelegateWidget*>(editor)->GetText();
  23.  
  24. model->setData(index, value, Qt::EditRole);
  25. }
  26.  
  27. void ItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  28. {
  29. static_cast<DelegateWidget*>(editor)->setGeometry(option.rect);
  30. }
To copy to clipboard, switch view to plain text mode 

DelegateWidget
Qt Code:
  1. DelegateWidget::DelegateWidget(QWidget *parent, const QModelIndex &index) :
  2. QWidget(parent)
  3. ,TextEditBox(new QTextEdit)
  4. ,mText(index.data().toString())
  5. ,ImgDisplay(new QLabel)
  6. {
  7.  
  8. QVBoxLayout *vLayout =new QVBoxLayout(this);
  9. QPixmap px=static_cast<QPixmap>(index.data(Qt::DecorationRole));
  10. ImgDisplay->setPixmap(px);
  11. mText=index.data(Qt::EditRole).toString();
  12. TextEditBox->setText(mText);
  13. vLayout->addWidget(ImgDisplay);
  14. vLayout->addWidget(TextEditBox);
  15. }
  16.  
  17. QString DelegateWidget::GetText() const {
  18. return mText;
  19. }
  20.  
  21.  
  22. void DelegateWidget::setText(const QString & text){
  23. mText=text;
  24. TextEditBox->setText(text);
  25. }
  26.  
  27. void DelegateWidget::updateText(void){
  28. mText = TextEditBox->toPlainText();
  29. //emit textChanged(mText);
  30. }
To copy to clipboard, switch view to plain text mode 

Main
Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4.  
  5. QWidget window;
  6.  
  7. QLabel* title = new QLabel("Custom widgets on a QListWidget");
  8. title->setAlignment(Qt::AlignHCenter);
  9.  
  10. QListWidget* list = new QListWidget;
  11. list->setItemDelegate(new ItemDelegate(list));
  12. list->setViewMode(QListView::IconMode);
  13. list->setIconSize(QSize(200,200));
  14. list->setWrapping(false);
  15.  
  16.  
  17. first->setText("1");
  18. first->setIcon(QPixmap("/Users/Nick/Desktop/chocolate.jpeg"));
  19. first->setFlags (first->flags () | Qt::ItemIsEditable);
  20. list->addItem(first);
  21.  
  22. second->setText("2");
  23. second->setIcon(QIcon("/Users/Nick/Desktop/chocolate.jpeg"));
  24. second->setFlags (second->flags () | Qt::ItemIsEditable);
  25. list->addItem(second);
  26.  
  27. QVBoxLayout* layout = new QVBoxLayout(&window);
  28. layout->addWidget(title);
  29. layout->addWidget(list);
  30. window.setLayout(layout);
  31.  
  32. window.show();
  33.  
  34. return a.exec();
  35. }
To copy to clipboard, switch view to plain text mode 

I got most of this code from another post somewhere in the forum (can't find it anymore though)
Thank you