Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取相对于QTreeWidget上项目的放置位置_Qt_Drag And Drop_Qtreewidget_Qtreewidgetitem - Fatal编程技术网

获取相对于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

我有一个覆盖了dropEvent方法的自定义QTreeWidget类。 方法如下:

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,但实际上这是正确的方法,非常好!