gpt4 book ai didi

qt - 获取相对于 QTreeWidget 上的项目的放置位置

转载 作者:行者123 更新时间:2023-12-02 04:09:27 28 4
gpt4 key购买 nike

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

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

if (!droppedIndex.isValid())
return;

// other logic

QTreeWidget::dropEvent(event);
}

我如何确定该项目是插入到其放置项目的上方、内部还是下方?

最佳答案

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

关于qt - 获取相对于 QTreeWidget 上的项目的放置位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37524292/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com