获取相对于QTreeWidget上项目的放置位置
我有一个覆盖了dropEvent方法的自定义QTreeWidget类。 方法如下:获取相对于QTreeWidget上项目的放置位置,qt,drag-and-drop,qtreewidget,qtreewidgetitem,Qt,Drag And Drop,Qtreewidget,Qtreewidgetitem,我有一个覆盖了dropEvent方法的自定义QTreeWidget类。 方法如下: void QCustomTreeWidget::dropEvent(QDropEvent * event) { QModelIndex droppedIndex = indexAt(event->pos()); if (!droppedIndex.isValid()) return; // other logic QTreeWidget::dropEve
void QCustomTreeWidget::dropEvent(QDropEvent * event)
{
QModelIndex droppedIndex = indexAt(event->pos());
if (!droppedIndex.isValid())
return;
// other logic
QTreeWidget::dropEvent(event);
}
如何确定该项目是插入到已放置项目的上方、内部还是下方?您需要使用。使用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
}
}
这个问题的大多数答案只是告诉我们使用ItemateEvent->pos,但实际上这是正确的方法,非常好!