我有一个自定义的QTreeWidget类,其中覆盖了dropEvent()
方法。
这是方法:
void QCustomTreeWidget::dropEvent(QDropEvent * event)
{
QModelIndex droppedIndex = indexAt(event->pos());
if (!droppedIndex.isValid())
return;
// other logic
QTreeWidget::dropEvent(event);
}
如何确定项目是否会插入项目的上方,内部或下方?
答案 0 :(得分:3)
您需要使用DropIndicatorPosition。使用switch
语句,您可以轻松实现您想要的目标。
bool bAbove = false; // boolean for the case when you are above an item
QModelIndex dropIndex = indexAt(event->pos());
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
if (!dropIndex.parent().isValid() && dropIndex.row() != -1)
{
switch (dropIndicator)
{
case QAbstractItemView::AboveItem:
// manage a boolean for the case when you are above an item
bAbove = true;
break;
case QAbstractItemView::BelowItem:
// something when being below an item
break;
case QAbstractItemView::OnItem:
// you're on an item, maybe add the current one as a child
break;
case QAbstractItemView::OnViewport:
// you are not on your tree
break;
}
if(bAbove) // you are above an item
{
// manage this case
}
}