I'm writing a delegate that I wish to use integrate with Drag and Drop on a QListView.
The idea is that if people drag a file from the desktop into the application the filename will be copied to the clipboard, then as they drag the mouse over items, either in the list, if they are hovering directly over the item, then when they release they will be asked if they want to replace the existing item, or if it's within a range of pixels from the top or bottom of the item, then I want to draw a red line between the items, the "insert" the dragged filename to this location.

As a start I've sub-classed QAbstractItemDelegate and implemented "paint" and "sizeHint".
I thought to begin with I'd just draw a straight line through the text on the selected item, so I hardcoded some values in, basically drawLine(left, height of item / 2, right, height of item / 2). This works fine, regardless of the item I click on the entire list gets cleared and the red line gets drawn through the first item.

Can anyone suggest what I need to add to the following code to draw through the item selected and retain the item data, I'm guessing I need to use the QModelIndex parameter.

Qt Code:
  1. #ifndef DRAGDROPLISTITEMDELEGATE_H
  2. #define DRAGDROPLISTITEMDELEGATE_H
  3.  
  4. #include <QAbstractItemDelegate>
  5.  
  6.  
  7. class DragDropListItemDelegate : public QAbstractItemDelegate
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. DragDropListItemDelegate(QObject* parent = 0);
  13.  
  14. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex &index) const;
  15. void setEditorData(QWidget* editor, const QModelIndex& index) const;
  16. void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
  17. void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const;
  18. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
  19. QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
  20.  
  21. };
  22.  
  23. #endif // DRAGDROPLISTITEMDELEGATE_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "DragDropListItemDelegate.h"
  2.  
  3. #include <QSpinBox>
  4. #include <QLineEdit>
  5. #include <QModelIndex>
  6. #include <QString>
  7. #include <QColor>
  8. #include <QPainter>
  9. #include <QSize>
  10.  
  11. DragDropListItemDelegate::DragDropListItemDelegate(QObject* parent)
  12. {
  13.  
  14. }
  15.  
  16. QWidget* DragDropListItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex &index) const
  17. {
  18. QLineEdit* editor = new QLineEdit(parent);
  19.  
  20. return editor;
  21. }
  22.  
  23. void DragDropListItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
  24. {
  25. QString value = index.model()->data(index, Qt::DisplayRole).toString();
  26. QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
  27. lineEdit->setText( value );
  28.  
  29. }
  30.  
  31. void DragDropListItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
  32. {
  33. QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
  34. QString value = lineEdit->text();
  35.  
  36. model->setData(index, value, Qt::EditRole);
  37. }
  38.  
  39. void DragDropListItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
  40. {
  41. editor->setGeometry(option.rect);
  42. }
  43.  
  44. void DragDropListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
  45. {
  46. painter->drawLine( option.rect.left(), option.rect.height() / 2, option.rect.right(), option.rect.height() / 2 );
  47. painter->setPen( QColor(255, 0, 0) );
  48.  
  49. }
  50.  
  51. QSize DragDropListItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
  52. {
  53. return QSize(10, 10);
  54. }
To copy to clipboard, switch view to plain text mode 

Also, the paint event is called in this case when I click an item. Will it also fire when I'm dragging over an item?