【问题标题】:Get the position of a drop relative to an item on a QTreeWidget获取相对于 QTreeWidget 上项目的放置位置
【发布时间】:2016-09-28 04:51:37
【问题描述】:

我有一个自定义 QTreeWidget 类,其中 dropEvent() 方法被覆盖。 方法如下:

void QCustomTreeWidget::dropEvent(QDropEvent * event)
{
    QModelIndex droppedIndex = indexAt(event->pos());

    if (!droppedIndex.isValid())
        return;

    // other logic

    QTreeWidget::dropEvent(event);
}

我如何确定该项目将插入它被丢弃的项目的上方、内部还是下方?

【问题讨论】:

    标签: qt drag-and-drop qtreewidget qtreewidgetitem


    【解决方案1】:

    您需要使用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
        }
    }
    

    【讨论】:

    • 这个问题的大多数答案只是告诉使用itemAt(event->pos()),但实际上这是正确的方法,非常好!
    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多