[英]qtreeview: drag and drop of a subtree results in bad selections
I've been trying to drag and drop a subtree in qtreeview. 我一直在尝试在qtreeview中拖放子树。 The tree correctly re-orders when I execute the drop and the underlying model is updated, however, item selection is then screwed up afterwords.
当我执行放置并更新基础模型时,树会正确地重新排序,但是,项目选择随后被弄乱了。 I haven't been able to come up with a reliable way to force QT to redraw the selection appropriately.
我无法提出一种可靠的方法来强制QT适当地重画选择。 See screenshots below:
查看以下屏幕截图:
Before: 之前:
After dragging the top node to node 2: 将顶部节点拖动到节点2:
Relevant view code: 相关查看代码:
void View::dropEvent(QDropEvent *evt)
{
QTreeView::dropEvent(evt);
QModelIndex start = indexAt(_drag_start);
QModelIndex end = indexAt(evt->pos());
_model->moveTo(start, end);
}
Relevant Model code: 相关型号代码:
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) );
}
Any suggestions? 有什么建议么? Thanks!
谢谢!
Turns out this is fixed by adding the following code in the Model::moveTo() method: 事实证明,通过在Model :: moveTo()方法中添加以下代码可以解决此问题:
...
emit layoutAboutToBeChanged();
list->moveTo( ... );
emit layoutChanged();
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.