I'm using a listview with a custom made model where the items in the listview are delegates.

I can't seem to get clicking on the list view to trigger selection changed. It looks like its because the delegate uses a label to render the information.

Here are some code bits:

This is the paint function in the delegate. The viewPtr is defined elsewhere and is a pointer to the view.

Qt Code:
  1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  2. const QModelIndex &index) const
  3. {
  4. painter->save();
  5.  
  6. std::cout << "Row: " << index.row() << std::endl;
  7.  
  8. if(viewPtr->indexWidget(index) == NULL)
  9. {
  10. QLabel *label = new QLabel();
  11. Data *data = qVariantValue<Data*>(index.data(Qt::DisplayRole));
  12. QString displayValue = data->displayValue();
  13. label->setAutoFillBackground(true);
  14. label->setWordWrap(true);
  15. label->setText(displayValue);
  16.  
  17.  
  18. if(option.state & QStyle::State_Selected)
  19. {
  20. //painter->fillRect(option.rect, option.palette.highlight());
  21. label->setPalette(option.palette);
  22. label->setBackgroundRole(QPalette::Highlight);
  23. viewPtr->setIndexWidget(index, label);
  24. QItemDelegate::paint(painter, option, index);
  25. }
  26. else
  27. {
  28. if(index.row() & 1)
  29. {
  30. //painter->fillRect(option.rect, QBrush(QColor("#c0c0c0")));
  31. p.setBrush(QPalette::Background, QBrush(QColor("#c0c0c0")));
  32. label->setPalette(p);
  33. label->setBackgroundRole(QPalette::Background);
  34. viewPtr->setIndexWidget(index, label);
  35. }
  36. else
  37. {
  38. //painter->fillRect(option.rect, QBrush(QColor("#dddddd")));
  39. p.setBrush(QPalette::Background, QBrush(QColor("#dddddd")));
  40. label->setPalette(p);
  41. label->setBackgroundRole(QPalette::Background);
  42. viewPtr->setIndexWidget(index, label);
  43. }
  44. QItemDelegate::paint(painter, option, index);
  45. }
  46. }
  47. painter->restore();
  48. }
To copy to clipboard, switch view to plain text mode 

I've tried the following to no avail:

Qt Code:
  1. MyDelegate delegate;
  2. delegate.setViewPointer(window->listView);
  3. window->listView->setItemDelegate(&delegate);
  4. QObject::connect(window->listView, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
  5. &delegate, SLOT(selected(const QModelIndex &current, const QModelIndex &)));
To copy to clipboard, switch view to plain text mode 

The delegate class has a selected slot that I've defined.

Where am I going wrong? The above code works if I'm not using a label...