Hi to all,
I'm having a problem with selection of an delegated item which represents two text lines and a CE_PushButton. I want to have the row selected by clicking the button too. Here is my paint function code:
Qt Code:
  1. void ColumnTwoLinesDelegate::paint(
  2. QPainter *painter,
  3. const QStyleOptionViewItem &option,
  4. const QModelIndex &index
  5. ) const
  6. {
  7. if (!index.isValid())
  8. return;
  9.  
  10. if (option.state & QStyle::State_Selected)
  11. {
  12. painter->setPen(QPen(Qt::white));
  13.  
  14. if (option.state & QStyle::State_Active)
  15. {
  16. painter->setBrush(QBrush(QPalette().highlight()));
  17. }
  18. else
  19. {
  20. painter->setBrush(QBrush(QPalette().color(QPalette::Inactive, QPalette::Highlight)));
  21. }
  22.  
  23. painter->drawRect(option.rect);
  24. }
  25. else
  26. {
  27. painter->setPen(QPen(Qt::black));
  28. }
  29.  
  30. QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
  31.  
  32. // ...it draws two text lines.
  33.  
  34. painter->restore();
  35.  
  36. QRect tButtonRect;
  37. tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
  38. tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
  39. tButtonRect.setWidth(kButtonSize);
  40. tButtonRect.setHeight(kButtonSize);
  41.  
  42.  
  43. if (index.model()->data(index.model()->index(index.row(), 5)).toInt() == 1)
  44. {
  45. tButton.icon = mArchive;
  46. }
  47. else
  48. {
  49. tButton.icon = mReActive;
  50. }
  51.  
  52. tButton.iconSize = QSize(kButtonSize - 2, kButtonSize - 2);
  53. tButton.rect = tButtonRect;
  54.  
  55. tButton.features |= QStyleOptionButton::Flat;
  56.  
  57. if (mCurrentRow == index.row())
  58. {
  59. tButton.state = QStyle::State_Sunken | QStyle::State_Enabled;
  60. }
  61. else
  62. {
  63. tButton.state = QStyle::State_Raised | QStyle::State_Enabled;
  64. }
  65.  
  66. QApplication::style()->drawControl(QStyle::CE_PushButton, &tButton, painter);
  67. }
To copy to clipboard, switch view to plain text mode 

and the editorEvent function code:

Qt Code:
  1. bool ColumnTwoLinesDelegate::editorEvent(
  2. QEvent *event,
  3. const QStyleOptionViewItem &option,
  4. const QModelIndex &index)
  5. {
  6. Q_UNUSED(model);
  7.  
  8. if (event->type() != QEvent::MouseButtonPress
  9. && event->type() != QEvent::MouseButtonRelease)
  10. {
  11. return true;
  12. }
  13.  
  14. // this rect represents a pushbutton
  15. QRect tButtonRect;
  16. tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
  17. tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
  18. tButtonRect.setWidth(kButtonSize);
  19. tButtonRect.setHeight(kButtonSize);
  20.  
  21. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  22.  
  23. // if mouse position is outside button rect/button area we use a State_Raised style
  24. if (!tButtonRect.contains(mouseEvent->pos()))
  25. {
  26. // it allows the row's selection
  27. return false;
  28. }
  29.  
  30. if (event->type() == QEvent::MouseButtonPress)
  31. {
  32. mCurrentRow = index.row();
  33. }
  34. else if (event->type() == QEvent::MouseButtonRelease)
  35. {
  36. mCurrentRow = -1;
  37.  
  38. // when MouseButtonRelease emit a signal like clicked()
  39. emit documentRequest(index);
  40. }
  41.  
  42. return true;
  43. }
To copy to clipboard, switch view to plain text mode 

only returning false allows the row selection, but I'd like to obtain the same behavior by clicking the button, is it possible?
Thanks in advance, Danilo