我在使用可以通过拖放重新排序的自定义项目实现 QListWidget 时遇到问题。问题是当我在一个项目上快速双击(非常短的拖放)时,该项目有时会从 QListWidget 中消失。
这是我的小部件的构造函数:
ListPopisiDragDrop::ListPopisiDragDrop(QWidget *parent) :
QListWidget(parent)
{
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDefaultDropAction(Qt::MoveAction);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);
}
还有 drop 事件:
void ListPopisiDragDrop::dropEvent(QDropEvent *event){
int startRow=currentIndex().row();
QListWidget::dropEvent(event);
int endRow=currentIndex().row();
//more code...
}
自定义项目是通过实现 QAbstractItemDelegate 中的 paint() 和 sizeHint() 函数来制作的。
当项目消失的问题发生时,甚至不会调用 dropEvent。
我真的不知道发生了什么以及我是否做错了什么。任何帮助表示赞赏。
谢谢!
编辑:我在 Symbian S60 第 5 版手机上运行该应用程序。
Edit2:如果我将此行添加到构造函数:
setDragDropOverwriteMode(true);
列表中的项目仍然消失,但空行留在原处。
Edit3:我添加了这段代码来看看发生了什么:
bool ListPopisiDragDrop::event(QEvent *e){
qDebug()<<"new event, type: "<<e->type()<<", listCount: "<<this->count();
QListWidget::event(e);
}
当调用 drop 事件时,我还打印了“drop event”。这给了我以下输出:
...
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 68 , listCount: 2
[Qt Message] DROPEVENT
[Qt Message] new event, type: 71 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 68 , listCount: 2
[Qt Message] DROPEVENT
[Qt Message] new event, type: 71 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 2
[Qt Message] new event, type: 68 , listCount: 2
[Qt Message] new event, type: 12 , listCount: 1
[Qt Message] new event, type: 12 , listCount: 1
[Qt Message] new event, type: 1 , listCount: 1
...
如您所见,在事件类型 68 之后,listCount 从 2 变为 1(一项消失)。我仍然没有找到问题所在......
Edit4:即使我不使用自定义项目,我也有相同的行为。仍然无法弄清楚出了什么问题。
Edit5:即使 [1] 中的示例在移动设备上进行测试时也具有相同的行为。Qt版本会不会有问题?我正在为 Symbian 设备版本 4.6.3 使用 Qt ...
[1]http://www.java2s.com/Code/Cpp/Qt/QListWidgetdraganddrop.htm