Hello folks,

I've got a little issue with my custom StyledItemDelegate. I have got a list view where I want to add a remove button on the first column. This works pretty good but it is just visible in edit mode, but I want to have it visible when I hover the row.

Can anyone give me a hint how to implement this?

buttondelegate.h
Qt Code:
  1. #ifndef BUTTONDELEGATE_H_
  2. #define BUTTONDELEGATE_H_
  3.  
  4. #include <QStyledItemDelegate>
  5.  
  6. class ButtonDelegate : public QStyledItemDelegate
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11. ButtonDelegate(QObject *aParent);
  12.  
  13. QWidget *createEditor(
  14. QWidget *aParent,
  15. const QStyleOptionViewItem &aOption,
  16. const QModelIndex &aIndex) const;
  17.  
  18. virtual void setEditorData(
  19. QWidget *aEditor,
  20. const QModelIndex &aIndex) const;
  21.  
  22. virtual void setModelData(
  23. QWidget *aEditor,
  24. const QModelIndex &aIndex) const;
  25.  
  26. virtual void updateEditorGeometry(
  27. QWidget *aEditor,
  28. const QStyleOptionViewItem &aOption,
  29. const QModelIndex &aIndex) const;
  30.  
  31. };
  32.  
  33. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "buttondelegate.h"
  2.  
  3. #include <QToolButton>
  4. #include <QApplication>
  5.  
  6. //-----------------------------------------------------------------------------
  7. ButtonDelegate::ButtonDelegate(QObject *aParent)
  8. : QStyledItemDelegate(aParent)
  9. {
  10. }
  11.  
  12. //-----------------------------------------------------------------------------
  13. QWidget *ButtonDelegate::createEditor(
  14. QWidget *aParent,
  15. const QStyleOptionViewItem & /*aOption*/,
  16. const QModelIndex &aIndex) const
  17. {
  18. if (aIndex.column() == 0) {
  19. QToolButton *btn = new QToolButton(aParent);
  20. btn->setText("...");
  21. //btn->setAutoRaise(true);
  22. btn->setCheckable(true);
  23. btn->setFixedSize(QSize(16, 16));
  24.  
  25. return btn;
  26. }
  27.  
  28. return NULL;
  29. }
  30.  
  31. //-----------------------------------------------------------------------------
  32. void ButtonDelegate::setEditorData(
  33. QWidget * /*aEditor */,
  34. const QModelIndex & /*aIndex*/) const
  35. {
  36. }
  37.  
  38. //-----------------------------------------------------------------------------
  39. void ButtonDelegate::setModelData(
  40. QWidget *aEditor,
  41. const QModelIndex &aIndex) const
  42. {
  43. //QApplication::processEvents();
  44. if (static_cast<QToolButton*>(aEditor)->isChecked())
  45. aModel->removeRow(aIndex.row());
  46. }
  47.  
  48. //-----------------------------------------------------------------------------
  49. void ButtonDelegate::updateEditorGeometry(
  50. QWidget *aEditor,
  51. const QStyleOptionViewItem &aOption,
  52. const QModelIndex & /*aIndex*/) const
  53. {
  54. aEditor->setGeometry(aOption.rect);
  55. }
To copy to clipboard, switch view to plain text mode 

And yes I have searched on the forum and found some things but they didn't help.

Kind regards,
Sven