QAbstractItemDelegate,editorEvent,CE_PushButton和项目选择

时间:2013-08-07 11:17:44

标签: c++ qt delegates

我遇到选择委托项目的问题,该项目代表两个文本行和一个CE_PushButton。我想通过单击按钮选择行。 这是我的 paint 功能代码:

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);
}

editorEvent 功能代码:

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;
}

只返回 false 允许行选择,但我想通过单击按钮获得相同的行为。可能吗? 提前致谢, 达尼洛

0 个答案:

没有答案