2016-05-30 61 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())',但實際上這是正確的做法,非常好! –

相關問題