我一直试图在qtreeview中拖放一个子树。当我执行drop并更新底层模型时,树正确地重新排序,然而,项目选择随后被搞砸了。我无法想出一种可靠的方法来强制QT适当地重新绘制选择。请参见下面的屏幕截图:
在:
将顶部节点拖动到节点2后:
相关观点代码:
void View::dropEvent(QDropEvent *evt)
{
QTreeView::dropEvent(evt);
QModelIndex start = indexAt(_drag_start);
QModelIndex end = indexAt(evt->pos());
_model->moveTo(start, end);
}
相关型号代码:
void Model::moveTo(QModelIndex& start, QModelIndex& end)
{
// ... omitted some error checking code to ensure start and end have the same parent
ModelData* a = (ModelData*) start.internalPointer();
ModelData* b = (ModelData*) end.internalPointer();
ModelData* list = (ModelData*) (parent(start).internalPointer());
int idxa = list->indexOf(a);
int idxb = list->indexOf(b);
// underneath this is QList<...>::moveTo
list->moveTo( idxa, idxb );
QModelIndex p = this->parent(start);
emit dataChanged( index(0, 0, p), index(rowCount(p)-1, 1, p) );
}
有什么建议吗?谢谢!
答案 0 :(得分:0)
结果证明这是通过在Model :: moveTo()方法中添加以下代码来解决的:
...
emit layoutAboutToBeChanged();
list->moveTo( ... );
emit layoutChanged();
...