1

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:

void ColumnTwoLinesDelegate::paint(
   QPainter *painter,
   const QStyleOptionViewItem &option,
   const QModelIndex &index
) const
{
   if (!index.isValid())
       return;

   if (option.state & QStyle::State_Selected)
   {
      painter->setPen(QPen(Qt::white));

      if (option.state & QStyle::State_Active)
      {
         painter->setBrush(QBrush(QPalette().highlight()));
      }
      else
      {
         painter->setBrush(QBrush(QPalette().color(QPalette::Inactive, QPalette::Highlight)));
      }

      painter->drawRect(option.rect);
   }
   else
   {
      painter->setPen(QPen(Qt::black));
   }

   QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);

   // ...it draws two text lines.

   painter->restore();

   QRect tButtonRect;
   tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
   tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
   tButtonRect.setWidth(kButtonSize);
   tButtonRect.setHeight(kButtonSize);

   QStyleOptionButton tButton;

   if (index.model()->data(index.model()->index(index.row(), 5)).toInt() == 1)
   {
      tButton.icon = mArchive;
   }
   else
   {
      tButton.icon = mReActive;
   }

   tButton.iconSize = QSize(kButtonSize - 2, kButtonSize - 2);
   tButton.rect = tButtonRect;

   tButton.features |= QStyleOptionButton::Flat;

   if (mCurrentRow == index.row())
   {
      tButton.state = QStyle::State_Sunken | QStyle::State_Enabled;
   }
   else
   {
      tButton.state = QStyle::State_Raised | QStyle::State_Enabled;
   }

   QApplication::style()->drawControl(QStyle::CE_PushButton, &tButton, painter);
}

and the editorEvent function code:

bool ColumnTwoLinesDelegate::editorEvent(
   QEvent *event,
   QAbstractItemModel *model,
   const QStyleOptionViewItem &option,
   const QModelIndex &index)
{
   Q_UNUSED(model);

   if (event->type() != QEvent::MouseButtonPress
      && event->type() != QEvent::MouseButtonRelease)
   {
      return true;
   }

   // this rect represents a pushbutton
   QRect tButtonRect;
   tButtonRect.setX(option.rect.width() - kHorizzontalMarginSize);
   tButtonRect.setY(option.rect.y() + (option.rect.height() / 2 - kButtonSize / 2));
   tButtonRect.setWidth(kButtonSize);
   tButtonRect.setHeight(kButtonSize);

   QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);

   // if mouse position is outside button rect/button area we use a State_Raised style
   if (!tButtonRect.contains(mouseEvent->pos()))
   {
      // it allows the row's selection
      return false;
   }

   if (event->type() == QEvent::MouseButtonPress)
   {
      mCurrentRow = index.row();
   }
   else if (event->type() == QEvent::MouseButtonRelease)
   {
      mCurrentRow = -1;

      // when MouseButtonRelease emit a signal like clicked()
      emit documentRequest(index);
   }

   return true;
}

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

4

0 回答 0