2016-05-30 5 views
0

dropEvent()メソッドをオーバーライドしたカスタムQTreeWidgetクラスがあります。ここ はメソッドです:QTreeWidget上のアイテムに対するドロップの位置を取得

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

    if (!droppedIndex.isValid()) 
     return; 

    // other logic 

    QTreeWidget::dropEvent(event); 
} 

どの項目が脱落している内部または項目の下に、上記挿入されます場合、私は確認できますか?

答えて

2

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 
    } 
} 
+0

この質問に対するほとんどの回答は 'itemAt(event-> pos())'を使用するように指示しますが、これは正しい方法です。 –

関連する問題