如何在带有委托的QTreeView中创建按钮?

时间:2018-08-15 06:46:07

标签: qt signals-slots qtreeview qstyleditemdelegate

我有一张这样的桌子:

enter image description here

当我将鼠标移到该表的一行上时,该行的右侧有一个按钮,当我单击该按钮时,该行将从表中删除。

enter image description here

我如何为此要求调整我的代表和信号/插槽?

void ItemDelegate::paint( QPainter *p_painter, const QStyleOptionViewItem &p_option, const QModelIndex &p_index ) const
{
  QStyleOptionViewItem option = p_option;

  QString text = p_index.data( Qt::DisplayRole ).toString();
  int col = p_index.column();

  if ( option.state & QStyle::State_MouseOver )
  {
      p_painter->fillRect( p_option.rect, Qt::green );
      p_painter->setPen(Qt::white);
  }
  else
  {
      p_painter->setPen(Qt::black);
  }

  if ( col == 0 )
  {
      option.rect.setRect( option.rect.left() + 20, option.rect.top(), option.rect.width() - 10, option.rect.height() );
  }
  else
  {
      option.rect.setRect( option.rect.left() + 10, option.rect.top(), option.rect.width() - 10, option.rect.height() );
  }

  p_painter->drawText( option.rect, Qt::AlignVCenter | Qt::TextWordWrap, text );
}

“ X”按钮的图像为x_button.png

0 个答案:

没有答案