QTablewidget删除而无需创建新行 [英] QTablewidget drop without creating new rows

查看:225
本文介绍了QTablewidget删除而无需创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QTableWidget,并且1列只有复选框,所以对于那些项目,我有以下标志:

I have a QTableWidget and 1 column has only checkboxes, so for those items I have these flags:

/* create prototype for checkbox item */ 
checkItem = new QTableWidgetItem();
Qt::ItemFlags flags = checkItem->flags();
flags &= ~Qt::ItemIsEditable;
flags &= ~Qt::ItemIsDropEnabled;
flags &= ~Qt::ItemIsDragEnabled;
flags |= Qt::ItemIsUserCheckable;
checkItem->setFlags(flags);
/**/

行得通...差不多.我不能在这些物品上放任何东西,很好.但是现在可以在2个单元格之间删除它们,因此创建了一个新行. 我该如何预防呢? 在其他启用了放置功能的单元格中,我只能放置这些单元格,而不能放置在它们之间,这很好.如果未启用该项目,为什么会更改此行为?

Ok that works... almost. I can't drop anything in those items, that's good. But now there can be dropped in between 2 cells, so there is a new row created. How can I prevent that? In the other columns where the cells are drop-enabled, I can only drop in the cells and not in between and that is good. Why is this behavior changed when the item is not drop-enabled?

推荐答案

使用事件过滤器的快速修改(可能需要一些调整):

A fast hack using an event filter (could need some tweaks):

您要做的是忽略复选框列上的任何放置.因此,禁用行创建就足够了.

What you do this is ignore any drop on the checkbox column. So it should be enough to disable row creation.

bool yourWidget::eventFilter(QObject *a_object, QEvent *a_event) 
  {
  bool result = false;
  if ((a_object == table->viewport()) && (a_event->type() == QEvent::Drop)) 
  {
     QDropEvent *p_drop_event = static_cast<QDropEvent *>(a_event);
     QPoint pos = p_drop_event->pos();
     QModelIndex new_index = table->indexAt(pos);
     if (new_index.column() == YOUR COLUMN HERE)
     {
       // Ignore drop event
       p_drop_event->setDropAction(Qt::IgnoreAction);
       p_drop_event->accept();
       return true;
     }
     else
     {
       // Allow drop
       return false;
     }

  }
return QObject::eventFilter(a_object, a_event);
}

有关eventFilters的信息:

Info about eventFilters:

事件过滤器

这篇关于QTablewidget删除而无需创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆