拖放 QListView 项目



请帮我解决这个问题....我在左侧有一个QListView,在另一侧有一个QWidgetQListView年,我使用QStandardItem添加了一些项目。现在我想将QListView项目拖放到另一侧QWidget,我也必须对QWidget执行相同的操作。我可以通过以下方式将QListView项目拖放QListView本身中

listView.setAcceptDrops(true);
listView.setDragEnabled(true);
listView.setDragDropMode(QAbstractItemView::InternalMove); 

这仅在QListView中工作正常。我想将QListView项目拖放到另一个小部件上。我该怎么做?我知道我必须处理这样的事件

void dropEvent(QDropEvent *);
void dragMoveEvent(QDragMoveEvent *);
void dragEnterEvent(QDragEnterEvent *);
void mousePressEvent(QMouseEvent *);

我只是这样试过

void Example::dragMoveEvent(QDragMoveEvent *e)
{
    // The event needs to be accepted here
    e->accept();
}
void Example::dragEnterEvent(QDragEnterEvent *e)
{
    // Set the drop action to be the proposed action.
    e->acceptProposedAction();
}
void Example::dropEvent(QDropEvent *e)
{
    qDebug("Items Dropped");
}

当我刚刚尝试使用一些 qDebug() 时,当我从QListView中拖动一个项目并将其放在QWidget中并得到输出为"项目已删除"时,这是有效的。但是我不知道如何把我的确切QListView的物品带到这里。

您不必对视图进行子类化。所有拖放内容都由模型处理,因此您必须对标准模型进行子类化。您可能需要查看模型子类化参考。当然,您还必须将视图的拖放模式更改为QAbstractItemView::DragDrop才能获得外部拖放。

最新更新