2014-01-29 47 views
0

按照這裏給出的建議:QTreeWidget reordering child items by dragging之後,拖動的項目未被選中。QTreeWidget拖放重新排序選擇錯誤的項目

所以,很自然地,我試着去拖動它,然後調用setSelected()。

結果是選擇正確之前的項目。

我繼承QTreeWidget覆蓋dropEvent這樣的 -

QTreeWidgetItem *pItem; 
QModelIndex dropIndex = indexAt(pEvent->pos()); 

if(dropIndex.isValid() == false) 
{ 
    pEvent->setDropAction(Qt::IgnoreAction); 
    pEvent->accept(); 
    return; 
} 

pItem = this->itemAt(pEvent->pos()); 
QTreeWidget::dropEvent(pEvent); 

我怎樣才能得到的指針正確QTreeWidgetItem已經下降了嗎?

+0

請問您可以顯示代碼在哪裏執行選擇? – vahancho

回答

1

由於丟棄的物品可能會「落在」目標物品的上方或下方,因此您需要管理這兩種情況並計算移動物品的正確索引。例如:

[..] 
virtual void dropEvent(QDropEvent * event) 
{ 
    QModelIndex droppedIndex = indexAt(event->pos()); 
    if(!droppedIndex.isValid()) 
     return; 

    QTreeWidget::dropEvent(event); 

    DropIndicatorPosition dp = dropIndicatorPosition(); 
    if (dp == QAbstractItemView::BelowItem) { 
     droppedIndex = droppedIndex.sibling(droppedIndex.row() + 1, // adjust the row number 
              droppedIndex.column()); 
    } 
    selectionModel()->select(droppedIndex, QItemSelectionModel::Select); 
}